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"]; 

音頻和視頻似乎對以這種方式進行本地導入的視頻正常工作後,進行更改。