How do I start playing audio when in silent mode & locked in iOS 6?

北城以北 提交于 2019-11-26 18:26:03

You need to make couple of changes in plist file.

i.e. 1) Set Required background mode to App plays audio

2) set Application does not run in background to YES.

 NSError *setCategoryErr = nil;
    NSError *activationErr  = nil;
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
    [[AVAudioSession sharedInstance] setActive:YES error:&activationErr];

Then, you need to write these much code in AppDelege

Now, you can easily run audio while phone screen locks or in background.

Had you previously been doing this in your app:

    AudioSessionInitialize (NULL, NULL, NULL, NULL);
    AudioSessionSetActive(true);

    // Allow playback even if Ring/Silent switch is on mute
    UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
    AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, 
                             sizeof(sessionCategory),&sessionCategory);

I had a user tell me that the audio wasn't working on an app (well before iOS5!). Turned out their ring/silent switch was set to "silent". So I added this code, and it causes the "silent" setting to be overridden. This is useful if you have a music app, for example, and you want to music to continue playing.

The long seeked solution to this could be a location based justification. Basically use the location background service to justify periodic updates to your app and thus trigger an alarm. This could be justified to apple by including a feature like Weather updates, which requires location services.

I'll look into this a bit into the future. Please if you have the time to look into it now, or if you have insight, dont hesitate to post.

Happy hunting.

Just a check, is your code missing this line:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

Have you registered your app as needing to use audio in the background?

From the docs:

A: I am using MPMoviePlayerController to play audio-only movies and other audio files on the iPhone in iOS 4. How do I ensure my audio will continue playing when my application is switched into the background or the screen is locked?

First, you must declare that your application supports background execution. An application does this by including the UIBackgroundModes key in its Info.plist file. This key identifies which background tasks your application supports. Its value is an array that contains one or more strings. Specify the string value audio to indicate the application plays audible content to the user while in the background.

Next, If you want to ensure that your movie audio continues when the screen locks, you need to assign an appropriate category to your audio session. You cannot rely on the default audio session, whose category (starting in iPhone OS 2.2) is AVAudioSessionCategorySoloAmbient (or equivalently, kAudioSessionCategory_SoloAmbientSound).

For playback to continue when the screen locks, or when the Ring/Silent switch is in the “silent” position, use the AVAudioSessionCategoryPlayback (or the equivalent kAudioSessionCategory_MediaPlayback) category. Listing 1 demonstrates how to initialize your application's audio session with the AVAudioSessionCategoryPlayback category.

Look into SystemSoundID; their audio plays at the volume of the "Ringer and Alerts" slider in Settings>Sounds despite the volume set by volume buttons and silent mode.

I notice that these apps are very clear about their requirements. They won't work unless (1) the user permits local notification alerts for this app, in Settings > Notifications, and (2) the app is frontmost when the user locks the screen (e.g., they won't work if the user clicks the Home button to quit the app and then locks the screen).

Therefore it's probably a combination of a local notification and a silent sound playing in the background. The silent sound means that when the screen is locked, if the app was frontmost, it is not suspended. The local notification is thus sent to the app itself and the app is able to respond by producing the alarm sound. Or perhaps there's no silent sound and the alarm sound you're hearing is just the custom sound attached to the local notification (but if so, then it isn't clear to me why the app would require to be frontmost when the screen is locked).

Also, though I haven't tested this, behaviour of the silent switch on the iPhone may have changed in 5.1 in response to the Mahler incident (http://www.nytimes.com/2012/01/13/nyregion/ringing-finally-stopped-but-concertgoers-alarm-persists.html). This would explain why developers are commenting that they could do this until 5.1.

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