且构网

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

iphone,即使在静音或静音模式下如何播放声音?

更新时间:2022-02-23 00:14:00

如果查看音频会话类别下的文档,您将找到许多模式,您可以设置这些模式以告诉系统您的应用计划如何使用音频。默认值为 AVAudioSessionCategorySoloAmbient ,它跟踪铃声/静音开关和屏幕锁定。

If you look in the docs under Audio Session Categories, you'll find a number of modes that you can set to tell the system how your app plans to use audio. The default is AVAudioSessionCategorySoloAmbient which tracks the ring/silent switch and the screen lock.

要让您的应用忽略响铃/静音开关设置,您可以尝试更改类别:

To have your app ignore the ring/silent switch settings, you could try changing the category:

#import <AudioToolbox/AudioToolbox.h>

AudioSessionInitialize (NULL, NULL, NULL, NULL);
AudioSessionSetActive(true);

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, 
                         sizeof(sessionCategory),&sessionCategory);

如果你想让iPod音频继续在后台播放,你还​​需要检查 kAudioSessionProperty_OverrideCategoryMixWithOthers

If you want to allow iPod audio to continue playing in the background, you'll also want to check kAudioSessionProperty_OverrideCategoryMixWithOthers.