AVAudioRecorder仅在中断后记录音频

Sid*_*rth 6 iphone objective-c avfoundation avaudiorecorder avaudiosession

在我使用AVAudioRecorder和AVAudioPlayer录制和播放音频的应用程序中,我遇到了拨打电话的情况.虽然录音正在进行中,但是如果打来电话,则只会录制通话后录制的音频.希望在通话后录制的录音是电话前录制的录音的延续.

我使用AVAudioRecorderDelegate方法跟踪录音机中发生的中断

  • (void)audioRecorderBeginInterruption:(AVAudioRecorder*)avRecorder和
  • (void)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];

}
Run Code Online (Sandbox Code Playgroud)

在寻找这个问题的解决方案时,我遇到了另一个链接 如何在iphone发生中断后恢复录制?http://www.iphonedevsdk.com/forum/iphone-sdk-development/31268-avaudiorecorderdelegate-interruption.html谈到了同样的问题.我尝试了这些链接中给出的建议,但没有成功.我希望它能与AVAudioRecorder本身一起使用.有什么方法可以找到解决这个问题的方法吗?所有宝贵的建议表示赞赏.

Sid*_*rth 5

经过几次研究后,Apple 通知我这是当前 API 的问题。因此,我设法通过在中断后立即保存先前的音频文件并将其与恢复的音频文件结合来找到解决该问题的方法。希望它可以帮助那些可能面临同样问题的人。