VLCPlaybackController+MediaLibrary.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.isPlaying) {
  39. //if nothing is playing start playing
  40. [self playMediaLibraryObject:mediaObject];
  41. return;
  42. }
  43. MLFile *newFile;
  44. if ([mediaObject isKindOfClass:[MLAlbumTrack class]]) {
  45. newFile = ((MLAlbumTrack *)mediaObject).anyFileFromTrack;
  46. } else if ([mediaObject isKindOfClass:[MLShowEpisode class]]) {
  47. newFile = ((MLShowEpisode *)mediaObject).anyFileFromEpisode;
  48. } else if ([mediaObject isKindOfClass:[MLFile class]]) {
  49. newFile = (MLFile *)mediaObject;
  50. }
  51. //if the newfile is not the currently playing one, stop and start the new one else do nothing
  52. VLCMedia *currentlyPlayingFile = self.currentlyPlayingMedia;
  53. MLFile *currentMLFile = [MLFile fileForURL:currentlyPlayingFile.url].firstObject;
  54. if (![currentMLFile isEqual:newFile]) {
  55. [self stopPlayback];
  56. [self playMediaLibraryObject:mediaObject];
  57. }
  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. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  73. if (![defaults boolForKey:kVLCAutomaticallyPlayNextItem]) {
  74. [self playMediaLibraryObject:showEpisode.files.anyObject];
  75. return;
  76. }
  77. NSArray *episodes = [[showEpisode show] sortedEpisodes];
  78. NSMutableArray *files = [NSMutableArray arrayWithCapacity:episodes.count];
  79. for (MLShowEpisode *episode in episodes) {
  80. MLFile *file = episode.files.anyObject;
  81. if (file)
  82. [files addObject:file];
  83. }
  84. int index = (int)[episodes indexOfObject:showEpisode];
  85. [self configureMediaListWithFiles:files indexToPlay:index];
  86. }
  87. - (void)configureWithAlbumTrack:(MLAlbumTrack *)albumTrack
  88. {
  89. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  90. if (![defaults boolForKey:kVLCAutomaticallyPlayNextItem]) {
  91. [self playMediaLibraryObject:albumTrack.anyFileFromTrack];
  92. return;
  93. }
  94. NSArray *tracks = [[albumTrack album] sortedTracks];
  95. NSMutableArray *files = [NSMutableArray arrayWithCapacity:tracks.count];
  96. for (MLAlbumTrack *track in tracks) {
  97. MLFile *file = track.anyFileFromTrack;
  98. if (file)
  99. [files addObject:file];
  100. }
  101. int index = (int)[tracks indexOfObject:albumTrack];
  102. [self configureMediaListWithFiles:files indexToPlay:index];
  103. }
  104. - (void)configureMediaListWithFiles:(NSArray *)files indexToPlay:(int)index
  105. {
  106. VLCMediaList *list = [[VLCMediaList alloc] init];
  107. VLCMedia *media;
  108. for (MLFile *file in files.reverseObjectEnumerator) {
  109. media = [VLCMedia mediaWithURL:file.url];
  110. [media addOptions:self.mediaOptionsDictionary];
  111. [list addMedia:media];
  112. }
  113. [self configureMediaList:list atIndex:index];
  114. }
  115. - (void)configureMediaList:(VLCMediaList *)list atIndex:(int)index
  116. {
  117. [self playMediaList:list firstIndex:index subtitlesFilePath:nil];
  118. }
  119. @end