VLCPlaybackController+MediaLibrary.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*****************************************************************************
  2. * VLCPlaybackController+MediaLibrary.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCPlaybackController+MediaLibrary.h"
  13. #import <CoreData/CoreData.h>
  14. @implementation VLCPlaybackController (MediaLibrary)
  15. /*
  16. Open a file in the libraryViewController and toggle the playstate
  17. @param mediaObject the object that should be openend
  18. */
  19. - (void)playMediaLibraryObject:(NSManagedObject *)mediaObject
  20. {
  21. self.fullscreenSessionRequested = YES;
  22. if ([mediaObject isKindOfClass:[MLFile class]]) {
  23. [self configureWithFile:(MLFile *)mediaObject];
  24. }
  25. else if ([mediaObject isKindOfClass:[MLAlbumTrack class]]) {
  26. [self configureWithAlbumTrack:(MLAlbumTrack *)mediaObject];
  27. self.fullscreenSessionRequested = NO;
  28. }
  29. else if ([mediaObject isKindOfClass:[MLShowEpisode class]])
  30. [self configureWithShowEpisode:(MLShowEpisode *)mediaObject];
  31. }
  32. /*
  33. Open a file in the libraryViewController without changing the playstate
  34. @param mediaObject the object that should be openend
  35. */
  36. - (void)openMediaLibraryObject:(NSManagedObject *)mediaObject
  37. {
  38. if (self.activePlaybackSession) {
  39. NSArray *files = [MLFile fileForURL:self.mediaPlayer.media.url];
  40. MLFile *nowPlayingFile = (MLFile *)(NSManagedObject *)files.firstObject;
  41. MLFile *newFile;
  42. if ([mediaObject isKindOfClass:[MLAlbumTrack class]]) {
  43. newFile = ((MLAlbumTrack *)mediaObject).anyFileFromTrack;
  44. } else if ([mediaObject isKindOfClass:[MLShowEpisode class]]) {
  45. newFile = ((MLShowEpisode *)mediaObject).anyFileFromEpisode;
  46. } else if ([mediaObject isKindOfClass:[MLFile class]]) {
  47. newFile = (MLFile *)mediaObject;
  48. }
  49. //if the newfile is not the currently playing one, stop and start the new one else do nothing
  50. if (![nowPlayingFile isEqual:newFile]) {
  51. [self stopPlayback];
  52. [self playMediaLibraryObject:mediaObject];
  53. }
  54. return;
  55. }
  56. //if nothing is playing start playing
  57. [self playMediaLibraryObject:mediaObject];
  58. }
  59. - (void)configureWithFile:(MLFile *)file
  60. {
  61. if (file.labels.count == 0) {
  62. [self configureMediaListWithFiles:@[file] indexToPlay:0];
  63. } else {
  64. MLLabel *folder = [file.labels anyObject];
  65. NSArray *files = [folder sortedFolderItems];
  66. int index = (int)[files indexOfObject:file];
  67. [self configureMediaListWithFiles:files indexToPlay:index];
  68. }
  69. }
  70. - (void)configureWithShowEpisode:(MLShowEpisode *)showEpisode
  71. {
  72. NSArray *episodes = [[showEpisode show] sortedEpisodes];
  73. NSMutableArray *files = [NSMutableArray arrayWithCapacity:episodes.count];
  74. for (MLShowEpisode *episode in episodes) {
  75. MLFile *file = episode.files.anyObject;
  76. if (file)
  77. [files addObject:file];
  78. }
  79. int index = (int)[episodes indexOfObject:showEpisode];
  80. [self configureMediaListWithFiles:files indexToPlay:index];
  81. }
  82. - (void)configureWithAlbumTrack:(MLAlbumTrack *)albumTrack
  83. {
  84. NSArray *tracks = [[albumTrack album] sortedTracks];
  85. NSMutableArray *files = [NSMutableArray arrayWithCapacity:tracks.count];
  86. for (MLAlbumTrack *track in tracks) {
  87. MLFile *file = track.anyFileFromTrack;
  88. if (file)
  89. [files addObject:file];
  90. }
  91. int index = (int)[tracks indexOfObject:albumTrack];
  92. [self configureMediaListWithFiles:files indexToPlay:index];
  93. }
  94. - (void)configureMediaListWithFiles:(NSArray *)files indexToPlay:(int)index
  95. {
  96. VLCMediaList *list = [[VLCMediaList alloc] init];
  97. VLCMedia *media;
  98. for (MLFile *file in files.reverseObjectEnumerator) {
  99. media = [VLCMedia mediaWithURL:file.url];
  100. [media addOptions:self.mediaOptionsDictionary];
  101. [list addMedia:media];
  102. }
  103. [self configureMediaList:list atIndex:index];
  104. }
  105. - (void)configureMediaList:(VLCMediaList *)list atIndex:(int)index
  106. {
  107. self.pathToExternalSubtitlesFile = nil;
  108. [self playMediaList:list firstIndex:index];
  109. }
  110. @end