简体   繁体   中英

Crash app when recording in background and get incoming call

I am using SFSpeechRecognizer work fine but when recording start in continues in background that time get incoming call app crash.

i am using this code

 AVAudioSession *session = [AVAudioSession sharedInstance];
    NSError *error = nil;
    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
    [session setMode:AVAudioUnitTypeMixer error:&error];


AVAudioFormat *format = [inputNode outputFormatForBus:0];
    [inputNode removeTapOnBus:0];

    if (inputNode != nil)
    {
        [inputNode installTapOnBus: 0 bufferSize: 8192 format: format block: ^(AVAudioPCMBuffer *buf, AVAudioTime *when)
         {
             [recognitionRequest appendAudioPCMBuffer:buf];
         }];
    }

 [audioEngine prepare];

error generate by xcode.

failed: '!pri' (enable 1, outf< 2 ch,      0 Hz, Float32, non-inter> inf< 1 ch,  44100 Hz, Float32>)
 [central] 54:   ERROR:    [0x1af844c40] >avae> AVAudioIONodeImpl.mm:883: SetOutputFormat: required condition is false: IsFormatSampleRateAndChannelCountValid(hwFormat)

You have to respond to app interruptions to prevent it from crashing. For example: Incase of an interrupt like incoming call,

After interruption starts Save state and context Update user interface

After interruption ends Restore state and context Reactivate audio session if app appropriate Update user interface

Please refer to link for further information Audio Session Programming Guide

For example,

 AVAudioSession *session=[AVAudioSession sharedInstance];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleInterruption:)
                                             name:AVAudioSessionInterruptionNotification
                                           object:session];
- (void)handleInterruption:(NSNotification *)notification{}

Once you handle this particular usecase, you should be good to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM