5

I'm using MPRemoteCommandCenter and MPMusicPlayerController.applicationMusicPlayer on the iPhone.

I'm trying to receive remote control events when the user is playing music and double taps on the headphone button.

If I use AVAudioPlayer, the remote commands are received perfectly.

However, if I use MPMusicPlayerController with any of its players (systemMusicPlayer, applicationMusicPlayer, or applicationQueuePlayer) the the commands do not get received. They appear to get gobbled up. For example when I double tap the remote, the music will toggle between play and stop. Instead, I need the remote events sent to my app.

Below is a sample app with my code. In the info.plist I've specified the required background mode for an app that plays audio (although its not necessary).

import UIKit
import MediaPlayer

class ViewController: UIViewController {
    var mpPlayer:MPMusicPlayerController!

    func remoteHandler() {
        print("success")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        mpPlayer = MPMusicPlayerController.applicationMusicPlayer()
        //mpPlayer = MPMusicPlayerController.systemMusicPlayer()
        assert(mpPlayer != nil)

        let cc = MPRemoteCommandCenter.shared()
        print("cc = \(cc)")

        cc.nextTrackCommand.isEnabled = true
        cc.nextTrackCommand.addTarget(self, action: #selector(ViewController.remoteHandler))
        cc.previousTrackCommand.isEnabled = true
        cc.previousTrackCommand.addTarget(self, action: #selector(ViewController.remoteHandler))
        cc.playCommand.isEnabled = true
        cc.playCommand.addTarget(self, action: #selector(ViewController.remoteHandler))

        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
            print("AVAudioSession successfully set AVAudioSessionCategoryPlayback")
        } catch let error as NSError {
            print("AVAudioSession setCategory error: \(error.localizedDescription)")
        }
        mpPlayer.setQueueWithStoreIDs(["270139033"]) // requires iOS 10.3
        mpPlayer.play()

    }
}

Output is:

cc = 0x123e086c0

AVAudioSession successfully set AVAudioSessionCategoryPlayback

remoteHandler is never called.
TylerH
  • 20,799
  • 66
  • 75
  • 101
salferrari
  • 81
  • 1
  • 7
  • did you find anything about this? I am also facing this issue and haven't found anything working for me. – umali Mar 22 '18 at 06:53
  • @umali nope. i'm still interested in a solution. if you figure something out please share. – salferrari Mar 23 '18 at 09:28
  • 1
    Any luck? I'm struggling to play Apple Music tracks, hence I can't use AV but have to use MP instead. But when using MP it's impossible (apparently) to get notification/handler/delegate/anything informing the user tapped a button in the lockscreen player. – Andres C Oct 30 '18 at 12:49
  • 1
    i was hoping that perhaps ios12 would fix this but i kinda moved on and accepted it as fate/haven't even revisited the issue in over a year. please let me know if you find a workaround. best wishes – salferrari Nov 02 '18 at 18:18

1 Answers1

1

From the Apple Developer web site.

When you use either the system or application player, you do not get event notifications. Those players automatically handle events.

So there is no way to receive remote control events if you use MPMusicPlayerController. Looking forward to see this feature! Right now MPMusicPlayerController is the only way to play Apple Music songs.