2

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

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

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

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

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

4

2 回答 2

10

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

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

我通过将该行替换为以下内容来修复它:

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

希望这会帮助别人!

于 2012-04-30T15:11:50.797 回答
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); 
于 2012-05-08T06:18:32.497 回答