最近,我开始在我的项目中添加音频,当你点击一个按钮,它会产生一个很好的声音效果,但即使我的iPhone的静音开关仍然在播放。我想知道如何检测用户已经切换到静音开关,如果是的话,完全静音应用的声音。
发布于 2016-12-05 18:37:11
您可能不需要实际测试静音开关是否打开,相反,只需告诉您的AudioSession哪个类别的播放模式是合适的,让iOS决定是否应该播放声音。
您需要AVAudioSessionCategoryAmbient,这将导致应用程序被静音按钮沉默。
发布于 2016-12-05 09:23:41
苹果没有任何直接的API来了解iPhones上的静默模式。然而,我们确实有一些工作来解决它。
以前的答案可以帮助你找到解决办法。
How to programmatically sense the iPhone mute switch?
How can I detect whether an iOS device is in silent mode or not?
用于SO answer的快速和工作解决方案。
// "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.");
}https://stackoverflow.com/questions/40968309
复制相似问题