Force audio alert to loud speaker

泄露秘密 提交于 2019-12-04 06:42:24

问题


I have a small app. In this app, the loud speaker makes noise every a certain time that I set up.

Now, I want it makes noise over it's built-in speaker even if a headset jack is plugged in the device.

How can I do this?


回答1:


you can try the below code to play code on speaker.

Also check the this

Hope this will help you.

[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];


UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;

AudioSessionSetProperty (
                         kAudioSessionProperty_OverrideAudioRoute,
                         sizeof (audioRouteOverride),
                         &audioRouteOverride
                         );



回答2:


You have to Override the Audio Route AFTER the headphones are plugged in. Your app can request an Audio Session Notification for when this occurs, and then do the Override again.




回答3:


This work for me.

+ (void)sessionAudioPort {
    UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
    AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
}


来源:https://stackoverflow.com/questions/24820061/force-audio-alert-to-loud-speaker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!