VLCPlaybackController+MediaLibrary.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #import <VLCMediaLibraryKit/VLCMLFile.h>
  15. @implementation VLCPlaybackController (MediaLibrary)
  16. /*
  17. Open a file in the libraryViewController and toggle the playstate
  18. @param mediaObject the object that should be openend
  19. */
  20. - (void)playMediaLibraryObject:(NSManagedObject *)mediaObject
  21. {
  22. self.fullscreenSessionRequested = YES;
  23. if ([mediaObject isKindOfClass:[MLFile class]]) {
  24. [self configureWithFile:(MLFile *)mediaObject];
  25. }
  26. else if ([mediaObject isKindOfClass:[MLAlbumTrack class]]) {
  27. [self configureWithAlbumTrack:(MLAlbumTrack *)mediaObject];
  28. self.fullscreenSessionRequested = NO;
  29. }
  30. else if ([mediaObject isKindOfClass:[MLShowEpisode class]])
  31. [self configureWithShowEpisode:(MLShowEpisode *)mediaObject];
  32. }
  33. - (void)playMedia:(VLCMLMedia *)media
  34. {
  35. [self configureMediaListWithMLMedia:@[media] indexToPlay:0];
  36. }
  37. /*
  38. Open a file in the libraryViewController without changing the playstate
  39. @param mediaObject the object that should be openend
  40. */
  41. - (void)openMediaLibraryObject:(NSManagedObject *)mediaObject
  42. {
  43. if (!self.isPlaying) {
  44. //if nothing is playing start playing
  45. [self playMediaLibraryObject:mediaObject];
  46. return;
  47. }
  48. MLFile *newFile;
  49. if ([mediaObject isKindOfClass:[MLAlbumTrack class]]) {
  50. newFile = ((MLAlbumTrack *)mediaObject).anyFileFromTrack;
  51. } else if ([mediaObject isKindOfClass:[MLShowEpisode class]]) {
  52. newFile = ((MLShowEpisode *)mediaObject).anyFileFromEpisode;
  53. } else if ([mediaObject isKindOfClass:[MLFile class]]) {
  54. newFile = (MLFile *)mediaObject;
  55. }
  56. //if the newfile is not the currently playing one, stop and start the new one else do nothing
  57. VLCMedia *currentlyPlayingFile = self.currentlyPlayingMedia;
  58. MLFile *currentMLFile = [MLFile fileForURL:currentlyPlayingFile.url].firstObject;
  59. if (![currentMLFile isEqual:newFile]) {
  60. [self stopPlayback];
  61. [self playMediaLibraryObject:mediaObject];
  62. }
  63. }
  64. - (void)configureWithFile:(MLFile *)file
  65. {
  66. if (file.labels.count == 0) {
  67. [self configureMediaListWithFiles:@[file] indexToPlay:0];
  68. } else {
  69. MLLabel *folder = [file.labels anyObject];
  70. NSArray *files = [folder sortedFolderItems];
  71. int index = (int)[files indexOfObject:file];
  72. [self configureMediaListWithFiles:files indexToPlay:index];
  73. }
  74. }
  75. - (void)configureWithShowEpisode:(MLShowEpisode *)showEpisode
  76. {
  77. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  78. if (![defaults boolForKey:kVLCAutomaticallyPlayNextItem]) {
  79. [self playMediaLibraryObject:showEpisode.files.anyObject];
  80. return;
  81. }
  82. NSArray *episodes = [[showEpisode show] sortedEpisodes];
  83. NSMutableArray *files = [NSMutableArray arrayWithCapacity:episodes.count];
  84. for (MLShowEpisode *episode in episodes) {
  85. MLFile *file = episode.files.anyObject;
  86. if (file)
  87. [files addObject:file];
  88. }
  89. int index = (int)[episodes indexOfObject:showEpisode];
  90. [self configureMediaListWithFiles:files indexToPlay:index];
  91. }
  92. - (void)configureWithAlbumTrack:(MLAlbumTrack *)albumTrack
  93. {
  94. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  95. if (![defaults boolForKey:kVLCAutomaticallyPlayNextItem]) {
  96. [self playMediaLibraryObject:albumTrack.anyFileFromTrack];
  97. return;
  98. }
  99. NSArray *tracks = [[albumTrack album] sortedTracks];
  100. NSMutableArray *files = [NSMutableArray arrayWithCapacity:tracks.count];
  101. for (MLAlbumTrack *track in tracks) {
  102. MLFile *file = track.anyFileFromTrack;
  103. if (file)
  104. [files addObject:file];
  105. }
  106. int index = (int)[tracks indexOfObject:albumTrack];
  107. [self configureMediaListWithFiles:files indexToPlay:index];
  108. }
  109. - (void)configureMediaListWithFiles:(NSArray *)files indexToPlay:(int)index
  110. {
  111. VLCMediaList *list = [[VLCMediaList alloc] init];
  112. VLCMedia *media;
  113. for (MLFile *file in files) {
  114. media = [VLCMedia mediaWithURL:file.url];
  115. [media addOptions:self.mediaOptionsDictionary];
  116. [list addMedia:media];
  117. }
  118. [self configureMediaList:list atIndex:index];
  119. }
  120. - (void)configureMediaListWithMLMedia:(NSArray<VLCMLMedia *> *)mlMedia indexToPlay:(int)index {
  121. VLCMediaList *list = [[VLCMediaList alloc] init];
  122. VLCMedia *media;
  123. for (VLCMLMedia *file in mlMedia) {
  124. media = [VLCMedia mediaWithURL: file.mainFile.mrl];
  125. [media addOptions:self.mediaOptionsDictionary];
  126. [list addMedia:media];
  127. }
  128. [self configureMediaList:list atIndex:index];
  129. }
  130. - (void)configureMediaList:(VLCMediaList *)list atIndex:(int)index
  131. {
  132. [self playMediaList:list firstIndex:index subtitlesFilePath:nil];
  133. }
  134. @end