VLCPlaybackController+MediaLibrary.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. if (self.activePlaybackSession) {
  32. self.sessionWillRestart = YES;
  33. [self stopPlayback];
  34. } else {
  35. self.sessionWillRestart = NO;
  36. [self startPlayback];
  37. }
  38. }
  39. /*
  40. Open a file in the libraryViewController without changing the playstate
  41. @param mediaObject the object that should be openend
  42. */
  43. - (void)openMediaLibraryObject:(NSManagedObject *)mediaObject
  44. {
  45. if (self.activePlaybackSession) {
  46. NSArray *files = [MLFile fileForURL:self.mediaPlayer.media.url];
  47. MLFile *nowPlayingFile = (MLFile *)(NSManagedObject *)files.firstObject;
  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. if (![nowPlayingFile isEqual:newFile]) {
  58. [self stopPlayback];
  59. [self playMediaLibraryObject:mediaObject];
  60. }
  61. return;
  62. }
  63. //if nothing is playing start playing
  64. [self playMediaLibraryObject:mediaObject];
  65. }
  66. - (void)configureWithFile:(MLFile *)file
  67. {
  68. if (file.labels.count == 0) {
  69. [self configureMediaListWithFiles:@[file] indexToPlay:0];
  70. } else {
  71. MLLabel *folder = [file.labels anyObject];
  72. NSArray *files = [folder sortedFolderItems];
  73. int index = (int)[files indexOfObject:file];
  74. [self configureMediaListWithFiles:files indexToPlay:index];
  75. }
  76. }
  77. - (void)configureWithShowEpisode:(MLShowEpisode *)showEpisode
  78. {
  79. NSArray *episodes = [[showEpisode show] sortedEpisodes];
  80. NSMutableArray *files = [NSMutableArray arrayWithCapacity:episodes.count];
  81. for (MLShowEpisode *episode in episodes) {
  82. MLFile *file = episode.files.anyObject;
  83. if (file)
  84. [files addObject:file];
  85. }
  86. int index = (int)[episodes indexOfObject:showEpisode];
  87. [self configureMediaListWithFiles:files indexToPlay:index];
  88. }
  89. - (void)configureWithAlbumTrack:(MLAlbumTrack *)albumTrack
  90. {
  91. NSArray *tracks = [[albumTrack album] sortedTracks];
  92. NSMutableArray *files = [NSMutableArray arrayWithCapacity:tracks.count];
  93. for (MLAlbumTrack *track in tracks) {
  94. MLFile *file = track.anyFileFromTrack;
  95. if (file)
  96. [files addObject:file];
  97. }
  98. int index = (int)[tracks indexOfObject:albumTrack];
  99. [self configureMediaListWithFiles:files indexToPlay:index];
  100. }
  101. - (void)configureMediaListWithFiles:(NSArray *)files indexToPlay:(int)index
  102. {
  103. VLCMediaList *list = [[VLCMediaList alloc] init];
  104. VLCMedia *media;
  105. for (MLFile *file in files.reverseObjectEnumerator) {
  106. media = [VLCMedia mediaWithURL:file.url];
  107. [media addOptions:self.mediaOptionsDictionary];
  108. [list addMedia:media];
  109. }
  110. [self configureMediaList:list atIndex:index];
  111. }
  112. - (void)configureMediaList:(VLCMediaList *)list atIndex:(int)index
  113. {
  114. self.pathToExternalSubtitlesFile = nil;
  115. [self playMediaList:list firstIndex:index];
  116. }
  117. @end