How to tell if Siri is active during app use

2019-06-28 08:15发布

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?

2条回答
乱世女痞
2楼-- · 2019-06-28 08:37

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

查看更多
劫难
3楼-- · 2019-06-28 08:44

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);
查看更多
登录 后发表回答