Detecting iPhone mute switch in iOS 5

后端 未结 1 2066
北海茫月
北海茫月 2021-01-01 00:50

I know that Apple does not provide a way to detect the state of the iPhone mute/silence switch in iOS 5. However, I have I tried a technique m

相关标签:
1条回答
  • 2021-01-01 01:06

    UPDATE: Sorry the below code posted works fine for me but I am using iOS4. For iOS5 this answer is what will solve your problem - Detecting the iPhone's Ring / Silent / Mute switch using AVAudioPlayer not working?

    This piece of code is what you need. Define a global gAudioSessionInited var. This flag tells you whether your mute switch is on or off.

    // "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)
提交回复
热议问题