问题
I have an audio app, and when AVAudioSessionRouteChangeNotification comes on, the app configures AV and tests for headphone use and such.
However, Siri activates the AVAudioSessionRouteChangeNotification, but because of the AV configuration, Siri isn't able to be used by the user (the microphone doesn't work for her, it seems.)
Is there a way to find out if Siri is the thing causing the AVAudioSessionRouteChangeNotification, so that I don't call the method to configure AV if it's Siri and thus let the user use Siri?
回答1:
You're right, both headphone plugging in/out and Siri activating will trigger AVAudioSessionRouteChangeNotification, however, notice the AVAudioSessionRouteChangeReasonKey associated with the notification
When headphone is plugged in, the reason is AVAudioSessionRouteChangeReasonNewDeviceAvailable
When headphone is plugged out, the reason is AVAudioSessionRouteChangeReasonOldDeviceUnavailable
When Siri is activated, the reason is AVAudioSessionRouteChangeReasonCategoryChange
回答2:
And use below code in AVAudioSessionRouteChangeNotification event if you want to check Siri start or finish:
AVAudioSessionRouteChangeReason routeChangeReason = [note.userInfo[AVAudioSessionRouteChangeReasonKey] integerValue];
AVAudioSessionRouteDescription* description = note.userInfo[AVAudioSessionRouteChangePreviousRouteKey];
BOOL isSiriStart = (routeChangeReason == AVAudioSessionRouteChangeReasonCategoryChange && description.inputs == nil);
BOOL isSiriFinish = (routeChangeReason == AVAudioSessionRouteChangeReasonCategoryChange && description.inputs != nil);
来源:https://stackoverflow.com/questions/28933944/how-to-tell-if-siri-is-active-during-app-use