AVComposition を使用して、2 つの .m4a オーディオ ファイルを結合しています。特定の時間に一方を他方に挿入する必要があります。次に、AVAssetExportSession を使用してマージ オーディオを保存します。
Export Session Completion Handler 中に次のエラーが発生します。
Error Domain=AVFoundationErrorDomain Code=-11820 "Cannot Complete Export" UserInfo=0x1c3760 {NSLocalizedRecoverySuggestion=Try exporting again., NSLocalizedDescription=Cannot Complete Export}
コード - 2 つのアセットのマージ:
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 に正常に記録され、ファイルに保存されます。