在我使用AVAudioRecorder和AVAudioPlayer錄製和播放音頻的應用程序中,我遇到了來電情況下的場景。在錄製過程中,如果來電,音頻錄製後只能錄製電話。我希望錄製電話後錄製的錄音成爲錄製電話前錄製的音頻的延續。AVAudioRecorder只錄制中斷後的音頻
我跟蹤使用AVAudioRecorderDelegate方法
在錄音機中斷存在的- (無效)audioRecorderBeginInterruption:(AVAudioRecorder *)avRecorder 和
- (無效)audioRecorderEndInterruption:(AVAudioRecorder *)avRecorder。
在我的EndInterruption方法中,我激活了audioSession。
這裏是記錄代碼,我使用
- (void)startRecordingProcess
{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&err];
if(err)
{
DEBUG_LOG(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
return;
}
[audioSession setActive:YES error:&err];
err = nil;
if(err)
{
DEBUG_LOG(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
return;
}
// Record settings for recording the audio
recordSetting = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInt:kAudioFormatMPEG4AAC],AVFormatIDKey,
[NSNumber numberWithInt:44100],AVSampleRateKey,
[NSNumber numberWithInt: 2],AVNumberOfChannelsKey,
[NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
nil];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:recorderFilePath];
if (fileExists)
{
BOOL appendingFileExists =
[[NSFileManager defaultManager] fileExistsAtPath:appendingFilePath];
if (appendingFileExists)
{
[[NSFileManager defaultManager]removeItemAtPath:appendingFilePath error:nil];
}
if (appendingFilePath)
{
[appendingFilePath release];
appendingFilePath = nil;
}
appendingFilePath = [[NSString alloc]initWithFormat:@"%@/AppendedAudio.m4a", DOCUMENTS_FOLDER];
fileUrl = [NSURL fileURLWithPath:appendingFilePath];
}
else
{
isFirstTime = YES;
if (recorderFilePath)
{
DEBUG_LOG(@"Testing 2");
[recorderFilePath release];
recorderFilePath = nil;
}
DEBUG_LOG(@"Testing 3");
recorderFilePath = [[NSString alloc]initWithFormat:@"%@/RecordedAudio.m4a", DOCUMENTS_FOLDER];
fileUrl = [NSURL fileURLWithPath:recorderFilePath];
}
err = nil;
recorder = [[recorder initWithURL:fileUrl settings:recordSetting error:&err]retain];
if(!recorder)
{
DEBUG_LOG(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
[[AlertFunctions sharedInstance] showMessageWithTitle:kAppName
message:[err localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"];
return;
}
//prepare to record
[recorder setDelegate:self];
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
}
在尋找一個解決這個問題我遇到另一個鏈接 how to resume recording after interruption occured in iphone?和http://www.iphonedevsdk.com/forum/iphone-sdk-development/31268-avaudiorecorderdelegate-interruption.html其中談到了同樣的問題來了。 我嘗試了這些鏈接中提供的建議,但沒有成功。 我希望能夠使用AVAudioRecorder本身。 有什麼辦法可以找到解決這個問題的方法嗎? 所有寶貴的建議表示讚賞。
請發帖,你是如何加入這兩個文件? –
您可以使用這兩個文件適當地創建一個AVComposition。下面的鏈接有如何做它的示例代碼http://stackoverflow.com/questions/7775040/play-avmutablecomposition-with-avplayer。之後,您可以使用AVAssetExportSession將樂曲導出爲單個音頻文件。通過這個鏈接http://stackoverflow.com/questions/8019033/avassetexportsession-not-working-in-ios5/9767681#9767681。希望這可以解決你的問題。 – Siddharth