好吧,也许我在这里漏掉了什么。我想在我的应用程序中使用黑色 Remote ,并基本上从 WWDC 2017 上关于这个问题的演讲中获得了这段代码。它说...
媒体播放的一致和直观控制是 tvOS 上许多应用程序的关键,正确使用和配置 MPNowPlayingInfoCenter 和 MPRemoteCommandCenter 对于提供出色的用户体验至关重要。深入研究这些框架,了解如何确保无缝体验,无论您的应用是使用 Siri、Siri Remote 还是 iOS Remote 应用控制。
所以我将这些行添加到我的 tvOS 应用程序的 viewDidLoad
并且它们基本上什么都不做?
var commandCenter = MPRemoteCommandCenter.shared()
override func viewDidLoad() {
super.viewDidLoad()
commandCenter.playCommand.isEnabled = true
commandCenter.pauseCommand.isEnabled = true
commandCenter.playCommand.addTarget { (commandEvent) -> MPRemoteCommandHandlerStatus in
print("You Pressed play")
return .success
}
commandCenter.pauseCommand.addTarget { (commandEvent) -> MPRemoteCommandHandlerStatus in
print("You Pressed pause")
return .success
}
}
我运行该应用程序,并尝试按下黑色 Remote 上的播放/暂停按钮,但调试控制台没有打印任何内容?还在 plist 中添加了一些与后台模式相关的代码...这应该有效还是我错过了这里的某个地方?
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>external-accessory</string>
</array>
最佳答案
当您的应用程序位于前台时,MPRemoteCommandCenter
中的命令不会由 Siri Remote 触发。要在前台时从远程获取事件,请像您可能已经习惯的那样使用 UIGestureRecognizer
。
MPRemoteCommandCenter
中的这些命令用于系统可能希望与您的播放交互的其他方式,例如:
您的应用正在后台播放音频,用户按下 Remote 上的暂停按钮:系统会要求您的应用暂停播放。
<用户正在使用适用于 iOS 的 TV Remote 应用,并且正在使用该应用的播放控制屏幕。
关于ios - 让 MPRemoteCommandCenter.shared() 在 tvOS 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44562925/