首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用户切换Mute开关时的静音

用户切换Mute开关时的静音
EN

Stack Overflow用户
提问于 2016-12-05 06:27:04
回答 2查看 243关注 0票数 0

最近,我开始在我的项目中添加音频,当你点击一个按钮,它会产生一个很好的声音效果,但即使我的iPhone的静音开关仍然在播放。我想知道如何检测用户已经切换到静音开关,如果是的话,完全静音应用的声音。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-05 18:37:11

您可能不需要实际测试静音开关是否打开,相反,只需告诉您的AudioSession哪个类别的播放模式是合适的,让iOS决定是否应该播放声音。

https://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategoriesandModes/AudioSessionCategoriesandModes.html

您需要AVAudioSessionCategoryAmbient,这将导致应用程序被静音按钮沉默。

票数 1
EN

Stack Overflow用户

发布于 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的快速和工作解决方案。

代码语言:javascript
复制
// "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.");
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40968309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档