使用MPMoviePlayerViewController从文档目录中显示视频

时间:2017-11-09 07:45:41

标签: ios objective-c video mpmovieplayercontroller

我正在尝试将视频保存到文档目录并再次播放。当我可以离线保存视频并在线下播放时,我正在使用youtube或类似hotstar的应用。

第1步:我从api获取视频网址,我将其转换为数据并使用此代码将其保存到文档目录。

-(void)downloadVideoAndSave :(NSString*)videoUrl videoname:(NSString *)name
 {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSLog(@"Downloading Started");
    NSString *urlToDownload =videoUrl;
    NSURL  *url = [NSURL URLWithString:urlToDownload];
    NSData *urlData = [NSData dataWithContentsOfURL:url];
    if ( urlData )
    {
        NSArray       *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString  *documentsDirectory = [paths objectAtIndex:0];

        NSString  *filePath = [NSString stringWithFormat:@"%@/%@%@", documentsDirectory,name,@".mp4"];

        //saving is done on main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            [urlData writeToFile:filePath atomically:YES];
            NSLog(@"File Saved !");
        });
    }

});
}

2.当用户处于离线状态时,我从文档目录中获取该视频,并且我正在尝试使用MPMoviePlayerViewController对它们进行操作。但它不能正常工作。我正在使用以下代码来执行此操作

-(void)playVideo:(UIButton*)sender {

NSURL *vedioURL;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
NSLog(@"files array %@", filePathsArray);

NSString *fullpath;
Store *info=[savedData objectAtIndex:sender.tag];

 NSString *videonamestr= info.videoname;
NSString *videofillname=[NSString stringWithFormat:@"/%@.mp4",videonamestr];
NSString  *filePath = [NSString stringWithFormat:@"%@/%@%@", documentsDirectory,videonamestr,@".mp4"];
 //fullpath = [documentsDirectory stringByAppendingPathComponent:videofillname];
// vedioURL =[NSURL fileURLWithPath:fullpath];
NSFileManager *fileManager = [NSFileManager defaultManager];

if ([fileManager fileExistsAtPath: fullpath]) //4
{
    NSLog(@"videoexists");
    //NSString *bundle = [[NSBundle mainBundle] pathForResource:filename ofType:@"plist"]; //5

    //[fileManager copyItemAtPath:bundle toPath:path error:&error]; //6
}
//    for ( NSString *apath in filePathsArray )
//    {
//        fullpath = [documentsDirectory stringByAppendingPathComponent:apath];
//        vedioURL =[NSURL fileURLWithPath:fullpath];
//    }
NSLog(@"vurl %@",filePath);

//    for (Store *items in savedData) {
//        [self downloadVideoAndSave:items.videopath];

//        NSLog(@"\n mobile data  %@\n",items.videopath);
//        
    UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    PlayerViewController *playerVC=[story instantiateViewControllerWithIdentifier:@"playerViewController"];
    playerVC.comingString = filePath;
    [self presentViewController:playerVC animated:NO completion:nil];

 // }


 }

3.在nextvc mpmoviewolayer

NSURL *fileUrl = [NSURL URLWithString:self.comingString];

self.moviePlayerViewController = [[MPMoviePlayerViewController alloc]initWithContentURL:fileUrl];

self.moviePlayerViewController.moviePlayer.fullscreen = NO;
[self.moviePlayerViewController.moviePlayer play];
self.moviePlayerViewController.moviePlayer.repeatMode = NO;
self.moviePlayerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;

[self.view addSubview:self.moviePlayerViewController.view];
[self presentViewController:self.moviePlayerViewController animated:NO completion:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

请帮我解决这个问题以及我犯错的地方。请不要在那种情况下记下来请回答。谢谢快速回复。

1 个答案:

答案 0 :(得分:0)

在你的玩家VC中这一行......

NSURL *fileUrl = [NSURL URLWithString:self.comingString];

应该是

[NSURL fileURLWithPath:self.comingString];