2016-12-05 189 views
0

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

回答

0

苹果公司没有任何直接的API来了解iPhone上的静音模式。但是我们确实有一些解决方法。

以前的答案可以帮助您找到解决方法。

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."); 
} 
+0

我看到....并且我在这里放置了这个代码来静音按钮的声音或整个应用程序的声音,如果静音被打开? –

+0

我把这个在viewdidload? –