// "Ambient" makes it respect the mute switch
// Must call this once to init session
if (!gAudioSessionInited)
{
AudioSessionInterruptionListener inInterruptionListener = NULL;
OSStatus error;
if ((error = AudioSessionInitialize (NULL, NULL, inInterruptionListener, NULL)))
{
NSLog(@"*** Error *** error in AudioSessionInitialize: %d.", error);
}
else
{
gAudioSessionInited = YES;
}
}
SInt32 ambient = kAudioSessionCategory_AmbientSound;
if (AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (ambient), &ambient))
{
NSLog(@"*** Error *** could not set Session property to ambient.");
}
2 回答
如果静音开关打开,您可能不需要实际测试,只需告诉您的AudioSession适合的类别播放模式,让iOS决定声音是否应该播放 .
https://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategoriesandModes/AudioSessionCategoriesandModes.html
您需要AVAudioSessionCategoryAmbient,这将导致应用程序被静音按钮静音 .
Apple没有任何直接API来了解iPhone上的静音模式 . 但是我们确实有一些工作可以搞清楚 .
以前的SO答案可以帮助您找到解决方法 .
How to programmatically sense the iPhone mute switch?
How can I detect whether an iOS device is in silent mode or not?
来自SO answer的快速且有效的解决方案 .