iOS 8.x Swift MPRemoteCommandCenter - 禁用音频播放控件

Tim*_*imD 5 audio playback ios swift

我目前正在遇到一个错误或缺乏对Apple文档的理解.

一些背景.我正在使用AVPlayer启动音频播放.我正在使用AVPlayer,因为它提供了更精确的播放定位控制(放入歌曲的特定位置).

当我实现下面的代码时,MPRemoteCommandCenter只显示一个按钮(播放/暂停按钮),并且所有其他按钮(后退和前进)都不存在.

  MPRemoteCommandCenter.sharedCommandCenter().playCommand.addTarget(self, action: "remoteCommandMute")
  MPRemoteCommandCenter.sharedCommandCenter().pauseCommand.addTarget(self, action: "remoteCommandMute")
Run Code Online (Sandbox Code Playgroud)

然后,我可以切换它是否被禁用或者不正常.根据Apple的文档,你应该能够切换启用的属性,它应该完全隐藏按钮而不必向他们添加目标(至少从我的理解).以下是文档的参考

如果我没有将.addTarget代码包含在任何按钮中,只需按照apple中的说明禁用所述按钮,它就不起作用.

    MPRemoteCommandCenter.sharedCommandCenter().previousTrackCommand.enabled = false
    MPRemoteCommandCenter.sharedCommandCenter().nextTrackCommand.enabled = false
    MPRemoteCommandCenter.sharedCommandCenter().playCommand.enabled = false
    MPRemoteCommandCenter.sharedCommandCenter().pauseCommand.enabled = false
Run Code Online (Sandbox Code Playgroud)

基于这种行为,我希望禁用所有三个按钮反过来会使它们隐藏,但这根本不是行为,实际上禁用它们对它们没有影响.您仍然可以按下它们,它们的行为就好像根本不存在该代码一样.但是,如果(如上所述)我将.addTarget添加到按钮,如果我将.enabled标志设置为false,它将被禁用.

如果这不是预期的行为,请纠正我的逻辑/理解,但在我看来,这绝对是苹果的反思.如果我的应用程序不支持暂停向前或向后功能,则该按钮根本不应出现并禁用它不应该只是将其变为灰色(就像它当前那样).如果你不向它们添加目标,它也不应该神奇地隐藏按钮.

另外,每当我向按钮添加目标时,即使我不希望我的应用程序具有前进和后退功能,它们也能正常工作.

Tim*_*imD 5

因此,在尝试了不同的可用按钮类型和操作后,似乎文档中的措辞可能会更好。为了“禁用和隐藏”按钮,只需将目标添加到所需按钮即可。例如,likeCommand.addTarget 禁用先前的暂停/播放和前进按钮并将它们隐藏。但是,如果我将 playCommand.enabled 设置为 false,它会将所述按钮显示为灰色。

编辑:为了解决实际上没有共享我所指的代码......

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()
Run Code Online (Sandbox Code Playgroud)

这表明了所概述的问题,即切换isEnabled属性与文档状态不同