VLCPlaybackController+MediaLibrary.m 4.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*****************************************************************************
 * 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>

@implementation VLCPlaybackController (MediaLibrary)

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

 @param mediaObject the object that should be openend
 
*/

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

39 40 41 42 43 44 45 46 47
/*
Open a file in the libraryViewController without changing the playstate

@param mediaObject the object that should be openend

*/

- (void)openMediaLibraryObject:(NSManagedObject *)mediaObject
{
48 49 50
    if (!self.isPlaying) {
        //if nothing is playing start playing
        [self playMediaLibraryObject:mediaObject];
51 52
        return;
    }
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    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];
    }
69 70
}

71 72 73
- (void)configureWithFile:(MLFile *)file
{
    if (file.labels.count == 0) {
74
        [self configureMediaListWithFiles:@[file] indexToPlay:0];
75 76 77 78 79 80 81 82
    } else {
        MLLabel *folder = [file.labels anyObject];
        NSArray *files = [folder sortedFolderItems];
        int index = (int)[files indexOfObject:file];
        [self configureMediaListWithFiles:files indexToPlay:index];
    }
}

83
- (void)configureWithShowEpisode:(MLShowEpisode *)showEpisode
84
{
85 86 87 88 89 90
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if (![defaults boolForKey:kVLCAutomaticallyPlayNextItem]) {
        [self playMediaLibraryObject:showEpisode.files.anyObject];
        return;
    }

91 92 93 94
    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
95
        if (file)
96 97 98 99
            [files addObject:file];
    }
    int index = (int)[episodes indexOfObject:showEpisode];
    [self configureMediaListWithFiles:files indexToPlay:index];
100 101 102 103
}

- (void)configureWithAlbumTrack:(MLAlbumTrack *)albumTrack
{
104 105 106 107 108 109
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if (![defaults boolForKey:kVLCAutomaticallyPlayNextItem]) {
        [self playMediaLibraryObject:albumTrack.anyFileFromTrack];
        return;
    }

110 111 112
    NSArray *tracks = [[albumTrack album] sortedTracks];
    NSMutableArray *files = [NSMutableArray arrayWithCapacity:tracks.count];
    for (MLAlbumTrack *track in tracks) {
113
        MLFile *file = track.anyFileFromTrack;
114 115
        if (file)
            [files addObject:file];
116 117 118 119 120 121 122 123
    }
    int index = (int)[tracks indexOfObject:albumTrack];
    [self configureMediaListWithFiles:files indexToPlay:index];
}

- (void)configureMediaListWithFiles:(NSArray *)files indexToPlay:(int)index
{
    VLCMediaList *list = [[VLCMediaList alloc] init];
124
    VLCMedia *media;
125
    for (MLFile *file in files) {
126 127 128
        media = [VLCMedia mediaWithURL:file.url];
        [media addOptions:self.mediaOptionsDictionary];
        [list addMedia:media];
129 130 131 132 133 134
    }
    [self configureMediaList:list atIndex:index];
}

- (void)configureMediaList:(VLCMediaList *)list atIndex:(int)index
{
135
    [self playMediaList:list firstIndex:index subtitlesFilePath:nil];
136 137 138
}

@end