Start playing MPMusicPlayerController when app is in background - ios

Is it possible to start playing 'MPMusicPlayerController' when the app is in a background state? I have an alarm clock app and I would like to start playing music when the alarm goes off and the app isn't in the foreground.

Go to project properties -> Capabilities and switch on the Application Background Modes
Then in your plist file add the following key:
Now just play the music in when your timer sets off.

Related

Background music continue to playing when open an app or change scene

I have game made with SpriteKit. When I open the app and I change the scene, while music is playing (for example from Music app on iPhone), the music stops. How can I continue to play music when I open the app and when I change scenes?
You need to configure your app's capabilities, and set the category on the Audio Session.
I think you're looking for:
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
There are other categories as well, but here are the docs on AVAudioSessionCategoryPlayback.
When using this category, your app audio continues with the Silent
switch set to silent or when the screen locks. (The switch is called
the Ring/Silent switch on iPhone.) To continue playing audio when your
app transitions to the background (for example, when the screen
locks), add the audio value to the UIBackgroundModes key in your
information property list file.
Here's a capture of the XCode Capabilities Panel:

Playing audio in a WatchKit workout app when backgrounded and screen is off

I'm trying to build a WatchKit app that will give audio feedback during a workout. The audio is a series of short pre-recorded voice clips.
The app starts a workout session
Audio clips play fine when the app is active
Audio also plays fine when the app is in the background whilst the screen is on (e.g. by tapping the digital crown, or opening another app).
However, when the screen turns off - e.g. by starting a workout and then lowering the wrist - the audio doesn't play until the app becomes active again.
i.e.
Start workout + keep app on screen = sound plays ok.
Start workout + move app to background by tapping digital crown + keep screen on = sound plays ok.
Start workout + lower wrist = screen switches off and sound no longer plays.
Start workout + move app to background + lower wrist = screen switches off and sound no longer plays.
I'm trying to figure out what I've missed in order to support audio in a workout app when the screen is off...
The app has an active HKWorkoutSession.
The extension has the "workout-processing" background mode enabled under WKBackgroundModes
The extension also has the "audio" background mode enabled under UIBackgroundModes.
The extension has the HealthKit entitlement and all HealthKit features are working.
On the iOS app, I've added the 'audio' background mode.
I've tried:
playing the audio clips using AVAudioPlayer
playing audio using AVAudioEngine+AVAudioPlayerNode
giving up on the clips entirely and using AVSpeechSynthesizer
The behaviour is the same - as soon as the screen switches off, the sound no longer plays. The sound does play if the app is in the background and the screen is still on.
The app is running on Watch OS 4. I've tested on a Series 2 and a Series 3 watch and it is the same on both.
What have I missed?
Ahhh, finally I found this post and realised I had the same problem:
The AVAudioSession category needs to be set to AVAudioSessionCategoryPlayback (I had it set to ambient).
As soon as I changed the session category to AVAudioSessionCategoryPlayback it worked!
To play audio while in background you also need to set the UIBackgroundModes to "audio" in the extension plist apart from setting the "workout processing" background mode.
Refrence HKWorkoutSession
To play audio or provide haptic feedback from the background, you must also add the UIBackgroundModes key to your WatchKit extension’s Info.plist file. This key’s value is an array containing the audio value.

How can I play music successively in the background with UIWebView or WKWebView

The website implemented inside my app would play a series of mp3 files,it works well when the app is active,but when I press HOME button or lock the phone,it cannont play more than ONE song in the background,after that,app suspended.
I've already tried to set different kind of AVAudioSession Category,but of no use.
I assume you don't have audio background mode enabled. This is what Apple documentation says:
When the UIBackgroundModes key contains the audio value, the system’s media frameworks automatically prevent the corresponding app from being suspended when it moves to the background. As long as it is playing audio or video content or recording audio content, the app continues to run in the background. However, if recording or playback stops, the system suspends the app.
So it seems like you just need to enable audio background mode. Here is Ray Wenderlich's tutorial on background modes

Play some sound in background, some sound only in foreground

I want to play sound in background for a feature but I want to play sound only in foreground for another feature.
Also, I want to play sound even when the silent switch is turned on.
To play sound in background, the following 2 code is needed:
[Info.plist] UIBackgroundModes = audio
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: [])
To play sound only in foreground, I think I have to set UIBackgroundModes to empty in the Info.plist.
But it is not possible while the app is running.
How can I play sound only in foreground for a feature and sound in background for another feature?
* playing sound in foreground means that sound is paused when the user push the home button of the iPhone and it is resumed when the user open the app again.
How can I play sound only in foreground for a feature and sound in background for another feature
Detect when the app goes into the background, and stop playing the foreground sound and switch to the background sound.

How to stop MPMusicPlayerController from playing when application terminate by user?

I have the following situation in my application:
I have a music system in my application and I used MPMusicPlayerController to play music; every thing is working fine until now.
My problem: When a user starts playing music in my application and after some time it terminates, music cannot be stopped because I'm using the [MPMusicPlayerController systemMusicPlayer] object. I know there is another option which is applicationMusicPlayer, but it stops playing music in background, which doesn't satisfy my requirements.
How can I stop the music from playing when the application is terminated by user?
I have some code that attempts to stop it in applicationWillTerminate: but it only works in some situations:
If I press home button twice and terminate the app from the multitasking UI, then the app can stop the music player.
If I press the home button once and then go to the home screen, and after that I press the home button twice and terminate the application then it can not stop my music player.
I tried to put a breakpoint in applicationWillTerminate: but in the second example (from above), the application crashed and did not execute my code, unlike in the first situation.
UPDAT
And I know that when I use MPMusicPlayerController background mode is not required because it starts music in the native music player.
Any help would be appreciated.
Unfortunately your only option based on your requirements is to play the audio using your own app, AVAudioPlayer for example and set the audio background mode requirement.
As there is no way of receiving a notification when your app is terminated by the OS or by the user, you have no way around it, you will not be able to stop the system or application player from your process.
Registering for NSUncaughtExceptionHandler wont catch SIGKILL or SIGSTOP either so you cannot rely on the system music player.
This shouldnt be a problem though as its easy to setup audio playback in your app. If you are using the system player so that the user can play their own music, you should use a picker and access their music library so that they can choose the music.
In this case:
If I press the home button once and then go to the home screen, and after that I press the home button twice and terminate the application then it can not stop my music player.
You have to register application in the background mode.
fileprivate var backgroundTask: UIBackgroundTaskIdentifier = .invalid
func registerBackgroundTask() {
self.backgroundTask = UIApplication.shared.beginBackgroundTask {
//TODO
}
}
Call it in AppDelegate
Hope to help you

Resources