Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 8.x Swift MPRemoteCommandCenter - Disabling audio playback controls

I'm currently experiencing either a bug or lack of understanding of Apple's Documentation.

Some background. I'm using AVPlayer to initiate a playback of audio. I'm using AVPlayer because it provides more precise controls over playback positioning (dropping into specific spots of a song).

When I implement the code below, the MPRemoteCommandCenter only displays one button (the play/pause button) and all other buttons (back and forward) are not present.

  MPRemoteCommandCenter.sharedCommandCenter().playCommand.addTarget(self, action: "remoteCommandMute")
  MPRemoteCommandCenter.sharedCommandCenter().pauseCommand.addTarget(self, action: "remoteCommandMute")

I can then toggle whether it's disabled or not just fine. According to Apple's documentation however you should be able to toggle the enabled property and it should hide the buttons entirely without having to add a target to them (at least from my understanding). Here is the reference to the documentation

If I do not include the .addTarget code to any of the buttons and simply follow the instructions from apple to disable said buttons, it does not work.

    MPRemoteCommandCenter.sharedCommandCenter().previousTrackCommand.enabled = false
    MPRemoteCommandCenter.sharedCommandCenter().nextTrackCommand.enabled = false
    MPRemoteCommandCenter.sharedCommandCenter().playCommand.enabled = false
    MPRemoteCommandCenter.sharedCommandCenter().pauseCommand.enabled = false

Based on this behavior I would expect that disabling all three buttons would in turn make them hidden however this is not the behavior at all in fact disabling them has no effect on them. You can still press them and they behave as if that code isn't present at all. However, if (as stated above) I add the .addTarget to the button it will become disabled if I set the .enabled flag to false.

Please correct my logic/understanding if this isn't the intended behavior but in my opinion this is absolutely backwards thinking on Apple's part. If my app doesn't support pausing forward or backward functionality the button shouldn't appear at all and disabling it shouldn't just turn it grey (like it currently does). It also shouldn't magically hide buttons if you don't add a target to them either.

On a side note, whenever I do add a target to the buttons they work just fine even if I don't want my app to have the forward and back capabilities.

like image 701
TimD Avatar asked May 07 '15 18:05

TimD


1 Answers

So after experimenting with the different available button types and actions, it seems that the wording in the documentation could be better. In order to "disable and hide" a button one simply needs to add a target to the desired button. For example, likeCommand.addTarget disables the previous pause/play and forward buttons and hides them. However, if I set the playCommand.enabled to false, it will display said button as greyed out.

Edit: To address not actually sharing the code to which I'm referring...

import UIKit
import PlaygroundSupport
import MediaPlayer
class MyViewController : UIViewController {
    private let remote: MPRemoteCommandCenter = MPRemoteCommandCenter.shared()
    override func loadView() {
        remote.playCommand.addTarget(self, action: #selector(playPause))
        remote.playCommand.isEnabled = false
    }

    @objc private func playPause() {

    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

This demonstrates the issue outlined such that toggling the isEnabled property does not do like the documentation states

like image 163
TimD Avatar answered Oct 01 '22 04:10

TimD