2016-12-05 7 views
0

最近、私は自分のプロジェクトにオーディオを追加し始めました。ボタンをタップすると素晴らしいサウンドエフェクトになりますが、iPhoneのミュートスイッチがオンの場合でもサウンドは再生されます。私はどのようにユーザーがミュートスイッチでトグルしたのかを知りたいのですが、その場合は完全にアプリケーションのサウンドをミュートしたいと思います。ユーザーがミュートスイッチをオンにすると音が鳴る

答えて

1

ミュートスイッチではなく、単に再生モードが適切であるかのカテゴリあなたのAudioSessionを伝え、サウンドが再生またはべきでない場合のiOSが決定させる、上にある場合は、実際にテストを必要としない場合があります。

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

あなたは、Appは、ミュートボタンによって消音されるようになりますどのAVAudioSessionCategoryAmbientをしたいです。

+0

ありがとう!トリックをやった! –

0

Appleは、iPhoneのサイレントモードを知るための直接的なAPIを持っていません。しかし、我々はそれを把握するためにいくつかの回避策を持っています。

前へSOの回答は、回避策を見つけるのに役立ちます。 SO answerから素早く作業溶液のために

How to programmatically sense the iPhone mute switch?

How can I detect whether an iOS device is in silent mode or not?

// "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に入れましたか? –

関連する問題