IPhone Mute Switch Detection in iOS 5

I know that Apple does not provide a way to detect iPhone mute/silence status in iOS 5 . However, I tried the method mentioned elsewhere to detect this by playing an audio file and measuring its execution time. However, even though my iPhone was turned off, the audio file was still playing for the entire duration. I use [AVAudioPlayer play] and calculates the time before calling audioPlayerDidFinishPlaying . Do you know about the trick for playing an audio file that will be completed earlier / immediately if the phone is disconnected?

+6
source share
1 answer

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

This piece of code you need. Define the 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."); } 
+4
source

Source: https://habr.com/ru/post/901654/


All Articles