0

我正在使用MPMediaPickerController来允许用户从设备上的库中选择视频和歌曲。我允许这个:initWithMediaTypes:MPMediaTypeAny初始化选择器。用户可以在导出后在应用内播放歌曲或视频。这里是我的电影出口代码,它剥离其核心功能后:从MPMediaPickerController导出视频 - 无音频

- (void)mediaPicker:(MPMediaPickerController*)mediaPicker didPickMediaItems:(MPMediaItemCollection*)mediaItemCollection { 
    AVAssetExportSession *exportSession; 
    NSString *filePath; 
    NSURL *fileUrl; 

    for (MPMediaItem *item in mediaItemCollection.items) { 
     NSURL *assetUrl = [item valueForProperty:MPMediaItemPropertyAssetURL]; 
     AVAsset *currAsset = [AVAsset assetWithURL:assetUrl]; 
     exportSession = [[AVAssetExportSession alloc] initWithAsset:[AVAsset assetWithURL:assetUrl] presetName:AVAssetExportPresetHighestQuality]; 
     exportSession.shouldOptimizeForNetworkUse = YES; 
     exportSession.outputFileType = AVFileTypeQuickTimeMovie; 
     filePath = [title stringByAppendingString:@".mov"]; 
     fileUrl = [NSURL fileURLWithPath:[[NSFileManager documentDirectory] stringByAppendingPathComponent:filePath]]; 

     exportSession.outputURL = fileUrl; 
     dispatch_group_enter(dispatchGroup); 

     [exportSession exportAsynchronouslyWithCompletionHandler:^{ 
      // success 
     } 
     dispatch_group_leave(dispatchGroup); 
    }]; 
    } 

这种类似的代码工作正常,这样做的音频,但对于视频,该视频的音频无法播放。 iTunes中的大部分内容都受保护且不可导出,所以我想用我用iPhone拍摄的自制快速视频进行测试。我拍摄了视频,并将其拖入iTunes中(并将其制作为“音乐视频”,以便它可以正常显示并可以导出到我的手机库)。然后我同步并将其发送到我的设备进行测试。

在应用程序中,视频在媒体选取器中显示正常,我可以将它导出而不会出现任何可以看到的错误。但是,当我在应用程序中播放它时,它只播放视频而不播放音频。我从其他来源导入的其他视频对播放视频音频效果不错,因此我不认为它是播放器本身。

有没有什么我可能会错过这里为什么音频不会从媒体选择器的这种出口出现?预先感谢在这个问题上的任何帮助!

回答

1

不知道这是否是理想的解决方案,但我们发现解决此问题的唯一方法是将其更改为使用PresetPassthrough设置强制m4v格式。 I.e:

exportSession = [[AVAssetExportSession alloc] initWithAsset:[AVAsset assetWithURL:assetUrl] presetName:AVAssetExportPresetPassthrough]; 
exportSession.shouldOptimizeForNetworkUse = YES; 
exportSession.outputFileType = AVFileTypeAppleM4V; 
filePath = [title stringByAppendingString:@".m4v"]; 

音频和视频似乎对以这种方式进行本地导入的视频正常工作后,进行更改。