Ios 让MPRemoteCommandCenter.shared()在tvOS中工作
好吧,也许我错过了什么。我想在我的应用程序中使用黑色遥控器,这段代码基本上来自WWDC 2017年关于这个问题的谈话。上面说 一致和直观地控制媒体播放是tvOS上许多应用程序的关键,正确使用和配置MPNowPlayingFocenter和MPRemoteCommandCenter对于提供出色的用户体验至关重要。深入了解这些框架,了解如何确保无缝体验,无论是使用Siri、Siri Remote还是iOS Remote应用程序控制您的应用程序 所以我将这些行添加到我的tvOS应用程序的Ios 让MPRemoteCommandCenter.shared()在tvOS中工作,ios,swift,tvos,mpremotecommandcenter,Ios,Swift,Tvos,Mpremotecommandcenter,好吧,也许我错过了什么。我想在我的应用程序中使用黑色遥控器,这段代码基本上来自WWDC 2017年关于这个问题的谈话。上面说 一致和直观地控制媒体播放是tvOS上许多应用程序的关键,正确使用和配置MPNowPlayingFocenter和MPRemoteCommandCenter对于提供出色的用户体验至关重要。深入了解这些框架,了解如何确保无缝体验,无论是使用Siri、Siri Remote还是iOS Remote应用程序控制您的应用程序 所以我将这些行添加到我的tvOS应用程序的viewDid
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
}
}
我运行应用程序,然后尝试黑色遥控器上的播放/暂停按钮,调试控制台上没有打印任何内容?还添加了一些与背景模式相关的plist代码…这是可行的,还是我遗漏了这里的要点
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>external-accessory</string>
</array>
ui背景模式
音频
外部附件
当应用程序位于前台时,Siri Remote不会触发MPRemoteCommandCenter
中的命令。要在前台从远程获取事件,请像您可能已经习惯的那样使用UIgestureRecognitizer
MPRemoteCommandCenter
中的这些命令用于系统可能希望与播放交互的其他方式,例如:
- 你的应用程序正在后台播放音频,用户按下遥控器上的暂停按钮:你的应用程序我将被要求暂停播放
- 用户正在使用iOS的TV Remote应用程序,并且正在使用该应用程序的播放控制屏幕
- 将问题发布到苹果支持部门;谁给我指出了正确的方向,我需要使用GCMicroGamepad控制器或其相关的GameKit框架。然后发现了2015年blauzahn发布的一个例子,他肯定应该为这篇文章赢得真正的赞誉。这是他为Swift 3.0和ios 10.x稍微修改的代码
import GameController
这并不是我在观看WWDC第251次会议时得到的印象,该次会议说“一致和直观地控制媒体播放是tvOS上许多应用程序的关键,无论您的应用程序是使用Siri、Siri Remote还是iOS Remote应用程序控制”……一致性是好的,也是可取的,要获得一致性,需要实现多个API。MPRemoteCommandCenter只是整个系统的一部分:它专门用于接收UIgestureRecognitor或其他“正常”应用程序内交互无法捕获的命令。您需要实现接受命令的两种方式。
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...")
}
}
}