How do I start playing audio when in silent mode &

2019-01-04 19:31发布

End-user process

  1. Open app
  2. Switch on 'Silent Mode'.
  3. Press 'Lock-button'
  4. App can still START playing a sound after hours have passed, not playing any audio in the interim.

Apps that do this

A lot of alarm apps have manage to do this & I dont think they are using silent audio to keep the app running as they do not sound if you actually exit the app with home.

  • Alarm Clock Pro
  • My Clock
  • Wave Alarm
  • Alarmed
  • iHome
  • ...

...Are they keeping a loop running after being locked some how or it a notification(which cant play sound in silent) starting the app back up to play the audio, or some other method?

Current Methods Implemented

AVAudioPlayer using:

AudioSessionInitialize(nil, nil, nil, nil);
AudioSessionSetActive(YES);

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof(sessionCategory),&sessionCategory);

And setting Info.plist to:

Required background modes(UIBackGroundModes) - App plays audio (audio)

At Present

I can play audio even in silent when the app is running and on screen. If the audio is already running the app can be closed with home button and audio will run. BUT if the app is not playing audio, and the screen is locked, all threads are killed and audio is never played. How do theses apps manage to work around this?

Possible approaches Found So Far

A. Use 'beginBackgroundTaskWithExpirationHandler:' with an infinite loop to keep app running indefinitely.

Pros:

  • Looks like you can make this work in a lot of situations, even when the user presses home.

Cons:

  • This goes against apple policies as far as I can tell.
  • will use more resources

Comments:

I've almost got this to work and might with some tweaking. This does not seem to be what all these other alarms are doing as they do not keep on running if you press home BTN. Which suggest that they use some method that gives them permission to run while locked but not in the BG. (Which is what I need)

Also, When you ask how much time you have left running you get aprox 10 min. By dropping an infinite loop in there the numbers will actually run down to 0 and then go into the negatives for hours on end.(tested) Im not sure how this would behave in the real world or in terms of app acceptance.

B. Use a silent audio loop to pose as a continus audio playing media center

Pros:

  • Worked when locked, and will keep up running in most situations.

Cons:

  • Can fail if interrupted by another media center and in some other occasions.
  • Can also go agains apple policy.

Comments:

This can work I a lot of situations but is by far not ideal. And since Like I say again, there has to be another method that is not documented.

Conclussions Thus Far

Testing with the listed APPs suggests that they are not using any of the two methods I just described. Method 'A' seems to be closer but if implemented would not behave how these apps behave.

I used a apple developer ticket to get more info, I'll post any new findings along those means as well.

Thank You

Any insight is appreciated, and for your participation thus far.

8条回答
劳资没心,怎么记你
2楼-- · 2019-01-04 19:39

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.

查看更多
【Aperson】
3楼-- · 2019-01-04 19:46

Just a check, is your code missing this line:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
查看更多
乱世女痞
4楼-- · 2019-01-04 19:51

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.

查看更多
叼着烟拽天下
5楼-- · 2019-01-04 19:52

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.

查看更多
不美不萌又怎样
7楼-- · 2019-01-04 19:53

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.

查看更多
登录 后发表回答