Detecting iPhone mute switch in iOS 5

痞子三分冷 提交于 2019-11-29 14:50:51

问题


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 mentioned elsewhere to detect this by playing an audio file and measuring its runtime. However, even though my iPhone was muted, the audio file still played the entire duration. I'm using [AVAudioPlayer play] and computing the time before audioPlayerDidFinishPlaying is called. Do you know of a trick to play an audio file, which will complete early/immediately if the phone is muted?


回答1:


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


来源:https://stackoverflow.com/questions/8158087/detecting-iphone-mute-switch-in-ios-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!