1
votes

I've been going through the audio session categories and overrides, and it appears that you can either play audio regardless of whether the silent switch is set or the screen is locked (AVAudioSessionCategoryPlayback), OR you can respect the silent switch and the screen lock settings (AVAudioSessionCategorySoloAmbient).

Any idea how I can play audio that gets muted by the silent switch, but keeps playing when the screen is locked? All I want is to have my cake and eat it too.

To answer the inevitable "why would you ever want to circumvent the system" questions, this is for an app that shows words and plays music which is likely to be used in both a church and home setting. At church it would be bad if the app started playing music, hence I want to respect the mute switch. At home you might set the phone down to listen to music while doing something else, and you wouldn't want it to stop playing when the phone auto-locks.

1
I will not say "why would you ever...", but I will say "If you want to respect the mute button, then you better start with respecting the church"medopal
The app shows words to hymns that are being sung at church :).Hilton Campbell

1 Answers

1
votes

You can use AVAudioSessionCategoryPlayback and use this code to check if silent switch is on or off:

- (BOOL)deviceIsSilenced
{
    #if TARGET_IPHONE_SIMULATOR
        // return NO in simulator. Code causes crashes for some reason.
        return NO;
    #endif

    CFStringRef state;
    UInt32 propertySize = sizeof(CFStringRef);
    AudioSessionInitialize(NULL, NULL, NULL, NULL);
    AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);

    return (CFStringGetLength(state) <= 0);
}