1

我收到了 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 文件时,我遇到了与将视频保存到相机胶卷有关的问题。

4

1 回答 1

-1

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

你的代码:

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

新代码:

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

SO 自动将 .mp4 转换为 .mov

于 2014-02-14T16:24:53.340 回答