7

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:

  1. If I press home button twice and terminate the app from the multitasking UI, then the app can stop the music player.

  2. 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.

Community
  • 1
  • 1
Chirag Shah
  • 3,034
  • 1
  • 30
  • 61
  • have you tried to add `audio` to `UIBackgroundModes` ? – Sega-Zero Jun 03 '15 at 12:27
  • @Sega-Zero when you play music via MPMusicPlayerController it not required the background modes – Chirag Shah Jun 03 '15 at 12:29
  • @chiragshah I am having the exact same issue - how did you resolve this? http://stackoverflow.com/questions/42331852/quitting-app-causes-error-message-from-debugger-terminated-due-to-signal-9 – wayneh Apr 12 '17 at 15:02

2 Answers2

3

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.

some_id
  • 29,466
  • 62
  • 182
  • 304
  • should you have any demo for this because i am not aware about how to play song in AVAudioPlayer – Chirag Shah Aug 21 '15 at 11:04
  • Sure, I will update my answer. First please explain what you want to play, where the audio comes from etc.? Such as if the user selects a song from their library, of if you present the user with a list of songs they can select from to play, are the songs on a server etc. ? – some_id Aug 21 '15 at 11:16
  • User can select the song which is in music player only – Chirag Shah Aug 21 '15 at 11:19
  • Ok, these should get you going. http://stackoverflow.com/questions/3191580/how-can-you-play-music-from-the-ipod-app-while-still-receiving-remote-control-ev, http://code.tutsplus.com/tutorials/ios-sdk_background-audio--mobile-6833 – some_id Aug 21 '15 at 11:57
0

In this case:

  1. 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