好吧,也许我漏掉了什么。我想用我的应用程序使用黑色遥控器,并从WWDC 2017关于这个问题的谈话中获得了这段代码。上面写着..。
对媒体播放的一致和直观的控制是tvOS上许多应用程序的关键,正确使用和配置MPNowPlayingInfoCenter和MPRemoteCommandCenter对于提供良好的用户体验至关重要。深入研究这些框架,了解如何确保您的应用程序是否使用Siri、Siri远程或iOS远程应用进行控制。
所以我在我的viewDidLoad of tvOS应用程序中添加了这些行,它们基本上什么也不做?
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
}
}我运行应用程序,并尝试在黑色遥控器上的播放/暂停按钮,什么都没有打印到调试控制台?还添加了一些代码,与背景mode...Should相关的plist,这项工作,还是我在某个地方遗漏了要点?
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>external-accessory</string>
</array>发布于 2017-06-15 15:59:54
当应用程序处于前台时,MPRemoteCommandCenter中的命令不是由Siri遥控器触发的。若要在前台从远程获取事件,请按照您可能已经习惯的方式使用UIGestureRecognizer。
MPRemoteCommandCenter中的这些命令用于系统可能希望与您的播放交互的其他方式,例如:
发布于 2017-06-24 09:52:08
向苹果支持部门发布了这个问题;谁为我指明了正确的方向,需要使用GCMicroGamepad控制器或相关的GameKit框架。比找到了一个2015年的例子布劳赞恩,谁最肯定值得赞扬,真正的这篇文章。以下是他为SWIFT3.0、ios 10.x所做的稍微修改的代码
import GameController。。
var gamePad: GCMicroGamepad? = nil
NotificationCenter.default.addObserver(self,
selector: #selector(gameControllerDidConnect),
name: NSNotification.Name.GCControllerDidConnect,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(gameControllerDidDisconnect),
name: NSNotification.Name.GCControllerDidDisconnect,
object: nil)
func gameControllerDidConnect(notification : NSNotification) {
if let controller = notification.object as? GCController {
if let mGPad = controller.microGamepad {
// Some setup
gamePad = mGPad
gamePad!.allowsRotation = true
gamePad!.reportsAbsoluteDpadValues = true
print("MicroGamePad connected...")
// Add valueChangedHandler for each control element
if gamePad?.buttonA.isPressed == true {
print("button A pressed")
}
if gamePad?.buttonX.isPressed == true {
print("button X pressed")
}
gamePad!.dpad.valueChangedHandler = { (dpad: GCControllerDirectionPad, xValue: Float, yValue: Float) -> Void in
print("dpad xValue = \(xValue), yValue = \(yValue)")
}
gamePad!.buttonA.valueChangedHandler = { (buttonA: GCControllerButtonInput, value:Float, pressed:Bool) -> Void in
print("\(buttonA)")
}
gamePad!.buttonX.valueChangedHandler = { (buttonX: GCControllerButtonInput, value:Float, pressed:Bool) -> Void in
print("\(buttonX)")
}
}
}
}
// Game controller disconnected
func gameControllerDidDisconnect(notification : NSNotification) {
if let controller = notification.object as? GCController {
if controller.microGamepad != nil {
self.gamePad = nil
print("MicroGamePad disconnected...")
}
}
}https://stackoverflow.com/questions/44562925
复制相似问题