MPMoviePlayerController无法播放新保存的视频文件 [英] MPMoviePlayerController doesn't play newly saved video file

查看:82
本文介绍了MPMoviePlayerController无法播放新保存的视频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用imagePickerController录制视频.在imagePickerController:didFinishPickingMediaWithInfo:函数中,我试图将视频设置为先前定义的MPMoviePlayerController:

I'm using an imagePickerController to record video. In the imagePickerController:didFinishPickingMediaWithInfo: function I'm trying to set the video to a previously defined MPMoviePlayerController:

if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]){
    NSURL *movieUrl = [info objectForKey:UIImagePickerControllerMediaURL];
    [self.moviePlayer setContentURL:movieUrl]];
}

这工作正常,并且确实在播放视频. 但是我想保存文件以备后用.当我这样做并将保存的文件用于moviePlayer时,什么也没发生:

This is working fine and the video is indeed playing. But I want to save the file for later use. When I do this and use the saved file for the moviePlayer, nothing happens:

  • 我已经尝试过(将数据保存到新文件中)

  • I've tried this (saving the data to a new file)

NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *newPath = [directory stringByAppendingPathComponent:@"myMovie.mov"];
NSData *videoData = [NSData dataWithContentsOfURL:movieUrl];
[videoData writeToFile:newPath atomically:NO];
[self.moviePlayer setContentURL:[NSURL URLWithString:newPath]];

  • 或此操作(将临时视频文件复制到我的文档文件夹中)

  • or this (copying the temp video file to my document folder)

    NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *newPath = [directory stringByAppendingPathComponent:@"myMovie.mov"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = nil;
    [fileManager copyItemAtPath:[videoUrl path] toPath:newPath error:&error];
    [self.moviePlayer setContentURL:[NSURL URLWithString:newPath]];
    

  • 即使newPath上的文件确实存在,也没有成功...我在做什么错了?

    Without any success, even though the file at newPath does exist... What am I doing wrong?

    推荐答案

    好,我终于找到了问题.问题实际上并非与MPMoviePlayerController有关,而与以下内容有关:

    Ok I finally found the issue. The problem was not in fact with the MPMoviePlayerController but with the line:

    [self.moviePlayer setContentURL:[NSURL URLWithString:newPath]];
    

    我通过将行替换为:

    [self.moviePlayer setContentURL:[NSURL fileURLWithPath:newPath isDirectory:NO]];
    

    希望对别人有帮助!

    这篇关于MPMoviePlayerController无法播放新保存的视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆