Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  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.

like image 379
chirag shah Avatar asked Jun 03 '15 11:06

chirag shah


1 Answers

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.

like image 129
some_id Avatar answered Sep 28 '22 13:09

some_id