问题描述
有什么办法记录到音频文件的结尾吗?我们不能暂停录制,而不是停止录制,因为用户需要能够回来到应用程序后,并添加更多的音频到他们的录音。目前,音频作为NSData存储在CoreData中。 NSData的 AppendData
不工作,因为生成的音频文件仍然报告它只与原始数据一样长。
Is there any way to record onto the end of an audio file? We can't just pause the recording instead of stopping it, because the user needs to be able to come back to the app later and add more audio to their recording. Currently, the audio is stored in CoreData as NSData. NSData's AppendData
does not work because the resulting audio file still reports that it is only as long as the original data.
另一种可能性是将原始音频文件与新的音频文件一起,并将它们连接成一个音频文件,如果有任何方法可以做到。
Another possibility would be taking the original audio file, along with the new one, and concatenate them into one audio file, if there's any way to do that.
推荐答案
这可以很容易地使用 AVMutableComposionTrack insertTimeRange:ofTrack:atTime:error
来完成。代码有点冗长,但它只是像4个步骤:
This can be done fairly easily using AVMutableComposionTrack insertTimeRange:ofTrack:atTime:error
. The code is somewhat lengthy, but it's really just like 4 steps:
// Create a new audio track we can append to
AVMutableComposition* composition = [AVMutableComposition composition];
AVMutableCompositionTrack* appendedAudioTrack =
[composition addMutableTrackWithMediaType:AVMediaTypeAudio
preferredTrackID:kCMPersistentTrackID_Invalid];
// Grab the two audio tracks that need to be appended
AVURLAsset* originalAsset = [[AVURLAsset alloc]
initWithURL:[NSURL fileURLWithPath:originalAudioPath] options:nil];
AVURLAsset* newAsset = [[AVURLAsset alloc]
initWithURL:[NSURL fileURLWithPath:newAudioPath] options:nil];
NSError* error = nil;
// Grab the first audio track and insert it into our appendedAudioTrack
AVAssetTrack *originalTrack = [originalAsset tracksWithMediaType:AVMediaTypeAudio];
CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, originalAsset.duration);
[appendedAudioTrack insertTimeRange:timeRange
ofTrack:[originalTrack objectAtIndex:0]
atTime:kCMTimeZero
error:&error];
if (error)
{
// do something
return;
}
// Grab the second audio track and insert it at the end of the first one
AVAssetTrack *newTrack = [newAsset tracksWithMediaType:AVMediaTypeAudio];
timeRange = CMTimeRangeMake(kCMTimeZero, newAsset.duration);
[appendedAudioTrack insertTimeRange:timeRange
ofTrack:[newTrack objectAtIndex:0]
atTime:originalAsset.duration
error:&error];
if (error)
{
// do something
return;
}
// Create a new audio file using the appendedAudioTrack
AVAssetExportSession* exportSession = [AVAssetExportSession
exportSessionWithAsset:composition
presetName:AVAssetExportPresetAppleM4A];
if (!exportSession)
{
// do something
return;
}
NSString* appendedAudioPath= @""; // make sure to fill this value in
exportSession.outputURL = [NSURL fileURLWithPath:appendedAudioPath];
exportSession.outputFileType = AVFileTypeAppleM4A;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
// exported successfully?
switch (exportSession.status)
{
case AVAssetExportSessionStatusFailed:
break;
case AVAssetExportSessionStatusCompleted:
// you should now have the appended audio file
break;
case AVAssetExportSessionStatusWaiting:
break;
default:
break;
}
NSError* error = nil;
}];
这篇关于AVAudioRecorder / AVAudioPlayer - 将记录附加到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!