1

我正在使用 AVComposition 将两个 .m4a 音频文件合并在一起。一个必须在特定时间插入另一个。然后我使用 AVAssetExportSession 来保存合并音频。

我在导出会话完成处理程序期间收到此错误:

Error Domain=AVFoundationErrorDomain Code=-11820 "Cannot Complete Export" UserInfo=0x1c3760 {NSLocalizedRecoverySuggestion=Try exporting again., NSLocalizedDescription=Cannot Complete Export}

代码 - 合并两个资产:

  AVAsset *originalAsset = [AVAsset assetWithURL:url];
NSURL *recordingAssetUrl = [[NSURL fileURLWithPath: NSTemporaryDirectory()] URLByAppendingPathComponent:kRecordTemp];
AVAsset *recordingAsset = [AVAsset assetWithURL:recordingAssetUrl];

AVMutableComposition *composition = [AVMutableComposition new];

AVMutableCompositionTrack *audioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio 
                                                                 preferredTrackID:kCMPersistentTrackID_Invalid];

//Add the original audio asset to the composition
AVAssetTrack *sourceAudioTrack = [[originalAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
NSError *error = nil;
CMTimeRange sourceRange = CMTimeRangeMake(CMTimeMakeWithSeconds(0, 1), originalAsset.duration);
[audioTrack insertTimeRange:sourceRange ofTrack:sourceAudioTrack atTime:CMTimeMakeWithSeconds(0, 1) error:&error];
if (error) [self showError:error];

//Remove the portion of the original audio asset that will be replaced by the recording
CMTimeRange overwrittenRange = CMTimeRangeMake(self.currentTime, recordingAsset.duration);
[audioTrack removeTimeRange:overwrittenRange];

//Add the recording asset to the composition
AVAssetTrack *recordAudioTrack = [[recordingAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
error = nil;
CMTimeRange recordRange = CMTimeRangeMake(CMTimeMakeWithSeconds(0, 1), recordingAsset.duration);
[audioTrack insertTimeRange:recordRange ofTrack:recordAudioTrack atTime:self.currentTime error:&error];
if (error) [self showError:error];

//Delete the original asset
if([[NSFileManager defaultManager] fileExistsAtPath:url.path]){
    error = nil;
    [[NSFileManager defaultManager] removeItemAtURL:url error:&error];
    if(error) [self showError:error];
}

//Write the composition
AVAssetExportSession * exportSession = [AVAssetExportSession exportSessionWithAsset:composition 
                                                                         presetName:AVAssetExportPresetAppleM4A]; 
exportSession.outputURL = url;
exportSession.outputFileType = AVFileTypeAppleM4A;

[exportSession exportAsynchronouslyWithCompletionHandler:^{
    if(exportSession.status != AVAssetExportSessionStatusCompleted){
        [self showError:exportSession.error];
    }
}];

//Delete the temporary asset
if([[NSFileManager defaultManager] fileExistsAtPath:recordingAssetUrl.path]){
    error = nil;
    [[NSFileManager defaultManager] removeItemAtURL:recordingAssetUrl error:&error];
    if(error) [self showError:error]; //BOOM it blows up here
}

我不确定我应该粘贴什么其他代码,因为它是一个通用错误。

我正在使用 AVAssetWriter 编写示例缓冲区以创建从捕获会话接收到的录制资产。捕获会话成功地记录并保存到文件到上面列表中使用的 url 中。

4

0 回答 0