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
Related
How to check iOS APP [music play app] is on Background when APP is launched by the headset wire control?
or quick music play like this
When we click on the quick song to play, in fact, the app has started and played the song, but the iOS system does not actively tell us that the program is in the background. In this case, how exactly do we know that the app is in the background and is separate from the normal home button background switch。
Or how do you distinguish this type of program startup
If you know when your app has started playing music (i.e. via a delegate method or callback or something), you can check the current state of the app by using UIApplication's applicationState property.
I am developing a project that play music in the background. Basically, the app will continue play the audio file even the screen is locked.
I have setup the background mode by selecting audio option, as you can see in this photo:
It works fine as I expected that the app keep playing the audio when users lock the screen.
However, when a call is received, the app stop playing. I thought it may start playing again after the phone call finished. However, it didn't.
I check a few webs, should I do some modification in the AppDelegate file, such as this method:
func applicationWillResignActive(_ application: UIApplication)
Basically, what I am trying to figure out is, how can I pause the audio when phone call or other notifications come and resume playing the audio when the phone call or other notifications finished.
Thanks in advance.
My application have permanent music inside that should play when it opened. Problem appears when alarm rang. It automatically stops app music, but it's not resumed after. Anyone know how can I handle this?
I think what I'm looking for is the combination of existing settings that I can't quite put together in the right order. I have an app that plays sound clips using AVAudioPlayer. There is a start/stop button.
I would like any other apps' audio to continue until my app actually needs to play something.
When my app is launched with some iTunes music playing, that music continues correctly. When I press play in my app, the other audio correctly stops, and my app plays. I stop my player, switch back to the music app, and press play again. But now when I switch back to my own app, the music stops immediately.
I would like it to again wait until it needs to play something before killing the music app.
My best guess is that once I've used the player, it is in some kind of 'I need audio' state, and remains in that state. How do I resign this state?
I use AudioQueue to play sound in my game. There are two sound objects: looped music and non-looped sfx. The problem that I observe is as follows:
The music is started.
The sfx is started.
The sfx is paused (using AudioQueuePause)
The game goes to background by Home Button.
I get UIApplicationWillResignActiveNotification and the music is paused (using AudioQueuePause).
I return to my app from iPad menu.
I get UIApplicationDidBecomeActiveNotification and the music is unpaused (using AudioQueueStart).
Note that I did not unpause sfx. In spite of that, it resumes and I can hear it!
I can only reproduce it on iPad with iOS 4.3 when going to background by Home Button. If I use Sleep/Wake button instead of Home, it works perfectly.
So it seems that the system somehow resumes all audio queues : even explicitly paused ones.
Any thoughts?