开发者

Any way to play audio on the iPhone when the screen is locked, but not when the silent switch is set?

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 (AVAudioSessionCategorySoloA开发者_Python百科mbient).

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.


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜