tags:

views:

3652

answers:

7

I can't seem to find in the SDK how to programatically sense the mute button/switch on the iPhone. When my app plays background music, it responds properly to the volume button without me having any code to follow that but, when I use the mute switch, it just keeps playing away.

How do I test the position of mute?

(NOTE: My program has its own mute switch, but I'd like the physical switch to override that.)

Thanks!

+4  A: 

Olie,

I believe you can find the answer to your question here:

https://devforums.apple.com/message/1135#1135

I'm assuming you have access to the Developer Forums at Apple.com :)

jpm
Sho' 'nuff!Quick recap: "AudioSessionSetProperty to set the AudioCategory to Ambient" (I believe it's ok to talk about shipping software...)Thanks!
Olie
+11  A: 

Thanks, JPM. Indeed, the link you provide leads to the correct answer (eventually. ;) For completeness (because S.O. should be a source of QUICK answers! )...

// "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.");
}
Olie
+1  A: 

Here two examples how to use AudioSessionInitialize: http://www.restoroot.com/Blog/2008/12/25/audiosessioninitialize-workarounds/

catlan
+5  A: 

I followed the general theory here and got this to work http://inforceapps.wordpress.com/2009/07/08/detect-mute-switch-state-on-iphone/

Here is a recap: Play a short silent sound. Time how long it takes to play. If the mute switch is on, the playing of the sound will come back much shorter than the sound itself. I used a 500ms sound and if the sound played in less than this time, then the mute switch was on. I use Audio Services to play the silent sound (which always honors the mute switch). This article says that you can use AVAudioPlayer to play this sound. If you use AVAudioPlayer, I assume you'll need to setup your AVAudioSession's category to honor the mute switch, but I have not tried it`.

Mark24x7
+1  A: 

To find out the state of the mute switch and the volume control I wrote these two functions. These are ideal if you wish to warn the user before they try creating audio output.

-(NSString*)audioRoute
{
    CFStringRef state;
    UInt32 propertySize = sizeof(CFStringRef);
    OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
    if( n )
    {
        // TODO: Throw an exception
        NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
    }

    NSString *result = (NSString*)state;
    [result autorelease];
    return result;
}

-(Float32)audioVolume
{
    Float32 state;
    UInt32 propertySize = sizeof(CFStringRef);
    OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareOutputVolume, &propertySize, &state);
    if( n )
    {
        // TODO: Throw an exception
        NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
    }
    return state;
}
Martin Cowie
+1  A: 

-(BOOL)isDeviceMuted { CFStringRef state; UInt32 propertySize = sizeof(CFStringRef); AudioSessionInitialize(NULL, NULL, NULL, NULL); AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state); return (CFStringGetLength(state) > 0 ? NO : YES); }

Haemish Graham
(CFStringGetLength(state) > 0 ? NO : YES) is the same as (CFStringGetLength(state) <= 0)
dkk
A: 

It doesn't work in my application...

Do I have to include something? I included the Audiotoolbox, but it still doesnt work... osString returns an error...

Thanks Markus

Markus