VLCPlaybackController+MediaLibrary.m 5.2 KB

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