2016-12-05 41 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? –

相關問題