1

I am trying to make audio playback controllable from the command center. The audio is being played from a view that has a new instance created every time a segue is triggered from the file list when a file is selected for playing.

The code that configures the command center controls is present in the viewDidLoad function of the player view:

        let audioSession = AVAudioSession.sharedInstance();
        do {
            try audioSession.setCategory(AVAudioSessionCategoryPlayback);
            try AVAudioSession.sharedInstance().setActive(true);
        } catch {}

        UIApplication.shared.beginReceivingRemoteControlEvents();

        TableViewController.commandCenter = MPRemoteCommandCenter.shared();

        TableViewController.commandCenter!.togglePlayPauseCommand.isEnabled = true;
        TableViewController.commandCenter!.togglePlayPauseCommand.addTarget(self, action: #selector(TableViewController.playbacker!.TogglePlayPause))

The first time I am entering the player view, the control center commands work perfectly fine. The problem arises when I am selecting another file for playback and the view is recreated. Then I am presented with the following exception:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_MPWeakInvocationTarget TogglePlayPause]: unrecognized selector sent to instance 0x170032340'

I have tried making the MPRemoteCommandCenter variable static, the player view reference static, but the results are the same.

Could anybody please give me some indication on why is this not working?

user2565010
  • 1,876
  • 4
  • 23
  • 37
  • I have also same issue. did you resolved this? – Dnyaneshwar Wakchaure Dec 06 '17 at 05:55
  • I haven't touched that code since January, but if I read the commit correctly, I moved the definitions specific to the command center to the `application`function of `AppDelegate.swift`. I made the first lines of the function be `UIApplication.shared.beginReceivingRemoteControlEvents();` and `commandCenter = MPRemoteCommandCenter.shared();` and then continued with enabling the buttons of the `commandCenter` variable. Let me know if it works out for you. – user2565010 Dec 08 '17 at 23:36
  • I have added first line UIApplication.shared.beginReceivingRemoteControlEvents(); In AppDelegate.swift still giving same error. Please check this 'https://stackoverflow.com/questions/47711137/mpweakinvocationtarget-pause-unrecognized-selector-sent-to-instance' – Dnyaneshwar Wakchaure Dec 11 '17 at 08:36

0 Answers0