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.
...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?
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)
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?
A. Use 'beginBackgroundTaskWithExpirationHandler:' with an infinite loop to keep app running indefinitely.
Pros:
Cons:
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:
Cons:
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.
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.
Any insight is appreciated, and for your participation thus far.
Note that enabling silent mode while watching a video or listening to music will immediately mute the sound playing. However, enabling this before you begin playing music or videos will not mute the sound.
Go to Settings > Sound & Haptics" or Settings > Sound to change if your phone vibrates while in Silent mode. Enable or disable "Change with Buttons" to change your ringtone and alert sound levels. If, for example, you don't have a ringtone selected, turning off Silent mode will not make your phone ring.
Silent mode is a setting available on mobile phones and pagers that, when activated, disables the ringtones and, in some cases, also the vibrating alerts or alarm. Unlike the airplane mode, the silent mode still allows the device to receive and send calls and messages.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With