且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AVAudioSession /音频会话服务切换输出

更新时间:2023-09-30 09:51:46

不管怎样,我不能保持扬声器输出,显然,设置属性的顺序很重要,虽然在文档中没有提到这一点。对于那些找到这最终,这是什么工作:

Apparently the order of setting properties matters, although there is no mention of this in the docs. For those that find this eventually, here's what works:

//Init and set the interrupt listener.  last parameter is passed to interruptlistener
AudioSessionInitialize(NULL, NULL, interruptlistener, NULL);

//Allow the app sound to continue to play when the screen is locked.
UInt32 audioCategory = kAudioSessionCategory_PlayAndRecord;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(audioCategory), &audioCategory);

//Force current audio out through speaker
UInt32 routeSpeaker = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(routeSpeaker), &routeSpeaker);

//Turn on the ability to mix with others
UInt32 doSetProperty = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);