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

2

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.

Gameto answered 3/6, 2015 at 11:58 Comment(3)
have you tried to add audio to UIBackgroundModes ?Cribb
@Cribb when you play music via MPMusicPlayerController it not required the background modesGameto
@chiragshah I am having the exact same issue - how did you resolve this? #42332352Intermarry
C
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.

Cobra answered 21/8, 2015 at 10:57 Comment(4)
should you have any demo for this because i am not aware about how to play song in AVAudioPlayerGameto
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. ?Cobra
User can select the song which is in music player onlyGameto
Ok, these should get you going. #3192080, code.tutsplus.com/tutorials/…Cobra
R
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

Rhamnaceous answered 14/2, 2019 at 4:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.