无法将.mp4视频文件保存到iphone相机胶卷?

时间:2013-07-23 15:25:33

标签: iphone ios objective-c media camera-roll

我收到了NSData格式的视频文件内容,我正在MPMoviePlayerController中成功播放该文件,代码如下

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

NSString *FileExtension=[[NSString alloc]init];

if([contentType hasPrefix:@"video"] || [contentType isEqualToString:@"application/video"]) {
    FileExtension=@"mov";
   //FileExtension=@"mp4";
}
else
{
  FileExtension=[contentType lastPathComponent];
}

 //appFile is type of NSString.
 appFile = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"MyFile.%@",FileExtension]];

//videoData is type of NSData
[videoData writeToFile:appFile atomically:YES]; 

NSURL *fileURL = [NSURL fileURLWithPath:appFile isDirectory:NO];

moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
moviePlayerController.view.frame = self.view.bounds;
moviePlayerController.movieSourceType = MPMovieSourceTypeFile;
moviePlayerController.fullscreen = YES;
moviePlayerController.shouldAutoplay=NO;

[self.view addSubview:moviePlayerController.view];
[moviePlayerController prepareToPlay];

但我无法将此视频文件写入/保存到相机胶卷,这是我将代码写入相机胶卷的代码。

  ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
  NSURL *filePathURL = [NSURL fileURLWithPath:appFile isDirectory:NO];
  if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:filePathURL]) {
    [library writeVideoAtPathToSavedPhotosAlbum:filePathURL completionBlock:^(NSURL *assetURL, NSError *error){
        if (error) {
            NSLog(@"Not success");
        } else {
            NSLog(@"success");
        }
    }];

} 

当我将mimeType / file_format设置为.mov&时,此代码工作正常。 .3Gp但是当mimeType / file_format为.mp4文件时,我遇到了将视频保存到相机胶卷的问题。

1 个答案:

答案 0 :(得分:-1)

如果您以这种方式更改appFile,则可以将.mp4转换为.mov:

您的代码:

 appFile = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"MyFile.%@",FileExtension]];

新代码:

 appFile = [documentsDirectory stringByAppendingPathComponent:@"MyFile.mov"];

SO自动将.mp4转换为.mov