MPMoviePlayerController不播放新保存的视频文件

时间:2012-04-04 13:48:27

标签: iphone ios video uiimagepickercontroller mpmovieplayercontroller

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

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

这工作正常,视频确实正在播放。 但我想保存文件供以后使用。当我这样做并将保存的文件用于moviePlayer时,没有任何反应:

  • 我试过这个(将数据保存到新文件)

    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]];
    
  • 或此(将临时视频文件复制到我的文档文件夹)

    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处的文件确实存在......我做错了什么?

2 个答案:

答案 0 :(得分:10)

好的,我终于找到了问题。问题实际上不是MPMoviePlayerController,而是行:

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

我用以下代码替换了该行:

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

希望能帮到别人!

答案 1 :(得分:1)

使用以下代码:

NSData *movieData;  
    NSError *dataReadingError = nil;        
    movieData = [NSData dataWithContentsOfURL: movieURL options:NSDataReadingMapped error:&dataReadingError];        
    if(movieData != nil)        
        NSLog(@"Successfully loaded the data.");   
    else        
        NSLog(@"Failed to load the data with error = %@", dataReadingError);