Commit d712f830 authored by Pierre SAGASPE's avatar Pierre SAGASPE

onedrive : external subtitles in all playing mode

parent 1676a0dc
......@@ -2,10 +2,11 @@
* VLCOneDriveObject.h
* VLC for iOS
*****************************************************************************
* Copyright (c) 2015 VideoLAN. All rights reserved.
* Copyright (c) 2015-2018 VideoLAN. All rights reserved.
* $Id$
*
* Authors: Felix Paul Kühne <fkuehne # videolan.org>
* Pierre Sagaspe <pierre.sagaspe # me.com>
*
* Refer to the COPYING file of the official project for license.
*****************************************************************************/
......@@ -51,6 +52,7 @@
@property (readonly, nonatomic) NSString *filesPath;
@property (strong, nonatomic) NSString *downloadPath;
@property (strong, nonatomic) NSString *subtitleURL;
@property (readonly, nonatomic) BOOL hasFullFolderTree;
@property (strong, nonatomic) LiveConnectClient *liveClient;
......
......@@ -2,10 +2,11 @@
* VLCOneDriveObject.h
* VLC for iOS
*****************************************************************************
* Copyright (c) 2015 VideoLAN. All rights reserved.
* Copyright (c) 2015-2018 VideoLAN. All rights reserved.
* $Id$
*
* Authors: Felix Paul Kühne <fkuehne # videolan.org>
* Pierre Sagaspe <pierre.sagaspe # me.com>
*
* Refer to the COPYING file of the official project for license.
*****************************************************************************/
......@@ -13,6 +14,7 @@
#import "VLCOneDriveObject.h"
#import "VLCHTTPFileDownloader.h"
#import "NSString+SupportedMedia.h"
#import "UIDevice+VLC.h"
#if TARGET_OS_IOS
@interface VLCOneDriveObject () <VLCHTTPFileDownloader>
......@@ -132,6 +134,10 @@
oneDriveObject.size = rawObject[@"size"];
oneDriveObject.thumbnailURL = rawObject[@"picture"];
oneDriveObject.downloadPath = rawObject[@"source"];
if (oneDriveObject.isVideo)
oneDriveObject.subtitleURL = [self configureSubtitle:oneDriveObject.name folderItems:rawFolderObjects];
oneDriveObject.duration = rawObject[@"duration"];
[folderFiles addObject:oneDriveObject];
}
......@@ -159,6 +165,68 @@
[self.delegate folderContentLoadingFailed:error sender:self];
}
#pragma - subtitle
- (NSString *)configureSubtitle:(NSString *)fileName folderItems:(NSArray *)folderItems
{
NSString *subtitleURL = nil;
NSString *subtitlePath = [self _searchSubtitle:fileName folderItems:folderItems];
if (subtitlePath)
subtitleURL = [self _getFileSubtitleFromServer:[NSURL URLWithString:subtitlePath]];
return subtitleURL;
}
- (NSString *)_searchSubtitle:(NSString *)fileName folderItems:(NSArray *)folderItems
{
NSString *urlTemp = [[fileName lastPathComponent] stringByDeletingPathExtension];
NSString *itemPath = nil;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", urlTemp];
NSArray *results = [folderItems filteredArrayUsingPredicate:predicate];
for (int cnt = 0; cnt < results.count; cnt++) {
NSDictionary *dictObject = results[cnt];
NSString *itemName = dictObject[@"name"];
if ([itemName isSupportedSubtitleFormat])
itemPath = dictObject[@"source"];
}
return itemPath;
}
- (NSString *)_getFileSubtitleFromServer:(NSURL *)subtitleURL
{
NSString *FileSubtitlePath = nil;
NSData *receivedSub = [NSData dataWithContentsOfURL:subtitleURL]; // TODO: fix synchronous load
if (receivedSub.length < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *directoryPath = searchPaths[0];
FileSubtitlePath = [directoryPath stringByAppendingPathComponent:[subtitleURL lastPathComponent]];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
//create local subtitle file
[fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
APLog(@"file creation failed, no data was saved");
return nil;
}
}
[receivedSub writeToFile:FileSubtitlePath atomically:YES];
} else {
VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), [subtitleURL lastPathComponent], [[UIDevice currentDevice] model]]
delegate:self
cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
otherButtonTitles:nil];
[alert show];
}
return FileSubtitlePath;
}
#pragma mark - delegation
- (void)folderContentLoaded:(VLCOneDriveObject *)sender
......
......@@ -2,10 +2,11 @@
* VLCOneDriveTableViewController.m
* VLC for iOS
*****************************************************************************
* Copyright (c) 2014-2015 VideoLAN. All rights reserved.
* Copyright (c) 2014-2018 VideoLAN. All rights reserved.
* $Id$
*
* Authors: Felix Paul Kühne <fkuehne # videolan.org>
* Pierre Sagaspe <pierre.sagaspe # me.com>
*
* Refer to the COPYING file of the official project for license.
*****************************************************************************/
......@@ -17,6 +18,7 @@
#import "VLCProgressView.h"
#import "UIDevice+VLC.h"
#import "NSString+SupportedMedia.h"
#import "VLCConstants.h"
@interface VLCOneDriveTableViewController () <VLCCloudStorageDelegate>
{
......@@ -106,110 +108,65 @@
[_oneDriveController loadCurrentFolder];
self.title = selectedObject.name;
} else {
VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
if (![[NSUserDefaults standardUserDefaults] boolForKey:kVLCAutomaticallyPlayNextItem]) {
/* stream file */
NSString *subtitlePath = [self _searchSubtitle:selectedObject.name];
subtitlePath = [self _getFileSubtitleFromServer:[NSURL URLWithString:subtitlePath]];
NSURL *url = [NSURL URLWithString:selectedObject.downloadPath];
VLCMediaList *medialist = [[VLCMediaList alloc] init];
[medialist addMedia: [VLCMedia mediaWithURL:url]];
[[VLCPlaybackController sharedInstance] playMediaList:medialist firstIndex:0 subtitlesFilePath:subtitlePath];
VLCMediaList *mediaList = [[VLCMediaList alloc] initWithArray:@[[VLCMedia mediaWithURL:url]]];
[self streamMediaList:mediaList startingAtIndex:0 subtitlesFilePath:selectedObject.subtitleURL];
} else {
NSMutableArray *mediaItems = [[NSMutableArray alloc] init];
NSInteger posIndex = 0;
NSUInteger counter = 0;
VLCMediaList *mediaList = [[VLCMediaList alloc] init];
for (VLCOneDriveObject *item in folderItems) {
if ((item.isFolder) || [item.name isSupportedSubtitleFormat])
continue;
NSURL *url = [NSURL URLWithString:item.downloadPath];
if (url) {
[mediaItems addObject:[VLCMedia mediaWithURL:url]];
[mediaList addMedia:[VLCMedia mediaWithURL:url]];
if (item.subtitleURL)
[[mediaList mediaAtIndex:counter] addOptions:@{ kVLCSettingSubtitlesFilePath : item.subtitleURL }];
counter ++;
if (item == selectedObject) {
posIndex = mediaItems.count -1;
}
if (item == selectedObject)
posIndex = mediaList.count - 1;
}
}
if (mediaItems.count > 0) {
[vpc playMediaList:[[VLCMediaList alloc] initWithArray:mediaItems] firstIndex:posIndex subtitlesFilePath:nil];
}
if (mediaList.count > 0)
[self streamMediaList:mediaList startingAtIndex:posIndex subtitlesFilePath:nil];
}
}
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (NSString *)_searchSubtitle:(NSString *)url
{
NSString *urlTemp = [[url lastPathComponent] stringByDeletingPathExtension];
NSArray *folderItems = _oneDriveController.currentFolder.items;
NSString *itemPath = nil;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", urlTemp];
NSArray *results = [folderItems filteredArrayUsingPredicate:predicate];
for (int cnt = 0; cnt < results.count; cnt++) {
VLCOneDriveObject *iter = results[cnt];
NSString *itemName = iter.name;
if ([itemName isSupportedSubtitleFormat])
itemPath = iter.downloadPath;
}
return itemPath;
}
- (NSString *)_getFileSubtitleFromServer:(NSURL *)subtitleURL
- (void)streamMediaList:(VLCMediaList *)mediaList startingAtIndex:(NSInteger)startIndex subtitlesFilePath:(NSString *)subtitlesFilePath
{
NSString *FileSubtitlePath = nil;
NSData *receivedSub = [NSData dataWithContentsOfURL:subtitleURL]; // TODO: fix synchronous load
if (receivedSub.length < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *directoryPath = searchPaths[0];
FileSubtitlePath = [directoryPath stringByAppendingPathComponent:[subtitleURL lastPathComponent]];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
//create local subtitle file
[fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
APLog(@"file creation failed, no data was saved");
return nil;
}
}
[receivedSub writeToFile:FileSubtitlePath atomically:YES];
} else {
VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), [subtitleURL lastPathComponent], [[UIDevice currentDevice] model]]
delegate:self
cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
otherButtonTitles:nil];
[alert show];
}
return FileSubtitlePath;
VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
vpc.fullscreenSessionRequested = NO;
[vpc playMediaList:mediaList firstIndex:startIndex subtitlesFilePath:subtitlesFilePath];
}
- (void)playAllAction:(id)sender
{
VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
NSUInteger counter = 0;
NSArray *folderItems = _oneDriveController.currentFolder.items;
NSMutableArray *mediaItems = [[NSMutableArray alloc] init];
VLCMediaList *mediaList = [[VLCMediaList alloc] init];
for (VLCOneDriveObject *item in folderItems) {
if ((item.isFolder) || [item.name isSupportedSubtitleFormat])
continue;
NSURL *url = [NSURL URLWithString:item.downloadPath];
if (url) {
[mediaItems addObject:[VLCMedia mediaWithURL:url]];
[mediaList addMedia:[VLCMedia mediaWithURL:url]];
if (item.subtitleURL)
[[mediaList mediaAtIndex:counter] addOptions:@{ kVLCSettingSubtitlesFilePath : item.subtitleURL }];
counter ++;
}
}
if (mediaItems.count > 0) {
[vpc playMediaList:[[VLCMediaList alloc] initWithArray:mediaItems] firstIndex:0 subtitlesFilePath:nil];
}
if (mediaList.count > 0)
[self streamMediaList:mediaList startingAtIndex:0 subtitlesFilePath:nil];
}
#pragma mark - login dialog
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment