VLCPlaybackController+MediaLibrary.m 5.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*****************************************************************************
 * VLCPlaybackController+MediaLibrary.m
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2015 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Tobias Conradi <videolan # tobias-conradi.de>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

#import "VLCPlaybackController+MediaLibrary.h"
#import <CoreData/CoreData.h>
15
#import <VLCMediaLibraryKit/VLCMLFile.h>
Soomin Lee's avatar
Soomin Lee committed
16
#import <VLCMediaLibraryKit/VLCMLMedia.h>
17 18 19

@implementation VLCPlaybackController (MediaLibrary)

20 21 22 23 24 25 26
/*
 Open a file in the libraryViewController and toggle the playstate

 @param mediaObject the object that should be openend
 
*/

27 28
- (void)playMediaLibraryObject:(NSManagedObject *)mediaObject
{
29
    self.fullscreenSessionRequested = YES;
30 31 32 33 34
    if ([mediaObject isKindOfClass:[MLFile class]]) {
        [self configureWithFile:(MLFile *)mediaObject];
    }
    else if ([mediaObject isKindOfClass:[MLAlbumTrack class]]) {
        [self configureWithAlbumTrack:(MLAlbumTrack *)mediaObject];
35
        self.fullscreenSessionRequested = NO;
36 37
    }
    else if ([mediaObject isKindOfClass:[MLShowEpisode class]])
38
        [self configureWithShowEpisode:(MLShowEpisode *)mediaObject];
39 40
}

41 42 43 44 45
- (void)playMedia:(VLCMLMedia *)media
{
    [self configureMediaListWithMLMedia:@[media] indexToPlay:0];
}

46 47 48 49 50 51 52 53 54
/*
Open a file in the libraryViewController without changing the playstate

@param mediaObject the object that should be openend

*/

- (void)openMediaLibraryObject:(NSManagedObject *)mediaObject
{
55 56 57
    if (!self.isPlaying) {
        //if nothing is playing start playing
        [self playMediaLibraryObject:mediaObject];
58 59
        return;
    }
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    MLFile *newFile;
    if ([mediaObject isKindOfClass:[MLAlbumTrack class]]) {
        newFile = ((MLAlbumTrack *)mediaObject).anyFileFromTrack;
    } else if ([mediaObject isKindOfClass:[MLShowEpisode class]]) {
        newFile = ((MLShowEpisode *)mediaObject).anyFileFromEpisode;
    } else if ([mediaObject isKindOfClass:[MLFile class]]) {
        newFile = (MLFile *)mediaObject;
    }

    //if the newfile is not the currently playing one, stop and start the new one else do nothing
    VLCMedia *currentlyPlayingFile = self.currentlyPlayingMedia;
    MLFile *currentMLFile = [MLFile fileForURL:currentlyPlayingFile.url].firstObject;
    if (![currentMLFile isEqual:newFile]) {
        [self stopPlayback];
        [self playMediaLibraryObject:mediaObject];
    }
76 77
}

78 79 80
- (void)configureWithFile:(MLFile *)file
{
    if (file.labels.count == 0) {
81
        [self configureMediaListWithFiles:@[file] indexToPlay:0];
82 83 84 85 86 87 88 89
    } else {
        MLLabel *folder = [file.labels anyObject];
        NSArray *files = [folder sortedFolderItems];
        int index = (int)[files indexOfObject:file];
        [self configureMediaListWithFiles:files indexToPlay:index];
    }
}

90
- (void)configureWithShowEpisode:(MLShowEpisode *)showEpisode
91
{
92 93 94 95 96 97
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if (![defaults boolForKey:kVLCAutomaticallyPlayNextItem]) {
        [self playMediaLibraryObject:showEpisode.files.anyObject];
        return;
    }

98 99 100 101
    NSArray *episodes = [[showEpisode show] sortedEpisodes];
    NSMutableArray *files = [NSMutableArray arrayWithCapacity:episodes.count];
    for (MLShowEpisode *episode in episodes) {
        MLFile *file = episode.files.anyObject;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
102
        if (file)
103 104 105 106
            [files addObject:file];
    }
    int index = (int)[episodes indexOfObject:showEpisode];
    [self configureMediaListWithFiles:files indexToPlay:index];
107 108 109 110
}

- (void)configureWithAlbumTrack:(MLAlbumTrack *)albumTrack
{
111 112 113 114 115 116
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if (![defaults boolForKey:kVLCAutomaticallyPlayNextItem]) {
        [self playMediaLibraryObject:albumTrack.anyFileFromTrack];
        return;
    }

117 118 119
    NSArray *tracks = [[albumTrack album] sortedTracks];
    NSMutableArray *files = [NSMutableArray arrayWithCapacity:tracks.count];
    for (MLAlbumTrack *track in tracks) {
120
        MLFile *file = track.anyFileFromTrack;
121 122
        if (file)
            [files addObject:file];
123 124 125 126 127 128 129 130
    }
    int index = (int)[tracks indexOfObject:albumTrack];
    [self configureMediaListWithFiles:files indexToPlay:index];
}

- (void)configureMediaListWithFiles:(NSArray *)files indexToPlay:(int)index
{
    VLCMediaList *list = [[VLCMediaList alloc] init];
131
    VLCMedia *media;
132
    for (MLFile *file in files) {
133 134 135
        media = [VLCMedia mediaWithURL:file.url];
        [media addOptions:self.mediaOptionsDictionary];
        [list addMedia:media];
136 137 138 139
    }
    [self configureMediaList:list atIndex:index];
}

140 141 142 143 144 145 146 147 148 149 150
- (void)configureMediaListWithMLMedia:(NSArray<VLCMLMedia *> *)mlMedia indexToPlay:(int)index {
    VLCMediaList *list = [[VLCMediaList alloc] init];
    VLCMedia *media;
    for (VLCMLMedia *file in mlMedia) {
        media = [VLCMedia mediaWithURL: file.mainFile.mrl];
        [media addOptions:self.mediaOptionsDictionary];
        [list addMedia:media];
    }
    [self configureMediaList:list atIndex:index];
}

151 152
- (void)configureMediaList:(VLCMediaList *)list atIndex:(int)index
{
153
    [self playMediaList:list firstIndex:index subtitlesFilePath:nil];
154 155 156
}

@end