2017-06-15 57 views
1

好吧,也许我错过了这里的东西。我想用我的应用程序使用黑色遥控器,并基本从WWDC 2017谈话中获得此代码。它说...让MPRemoteCommandCenter.shared()在tvOS中工作

一致和直观的媒体播放控制是许多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 
    } 
    } 

我运行应用程序,并尝试黑色遥控器并没有什么上的播放/暂停按钮被打印到调试控制台?还添加了一些与背景模式有关的plist代码...应该这样做还是我在这里错过了点?

<key>UIBackgroundModes</key> 
<array> 
    <string>audio</string> 
    <string>external-accessory</string> 
</array> 

回答

1

当您的应用处于前台时,Siri Remote不会触发MPRemoteCommandCenter中的命令。当您处于前台时,要从遥控器获取事件,请使用UIGestureRecognizer,就像您可能已经习惯的那样。

MPRemoteCommandCenter这些命令的其他方式的系统可能要与您的播放互动,如:

  • 您的应用播放音频在后台,并且用户按下暂停按钮在遥控器上:我的应用程序会被要求暂停播放。

  • 用户正在使用iOS的TV Remote应用程序,并且正在使用该应用程序的播放控制屏幕。

+0

这不是我的印象得到了看WWDC会议251,它说“的媒体一致的和直观的控制播放是tvOS上许多应用程序的关键,无论您的应用程序是使用Siri,Siri Remote还是iOS Remote应用程序进行控制“...? – user3069232

+0

一致性是好的和可取的,获得这种一致性需要你实现多个API。 MPRemoteCommandCenter只是整体的一部分:它专门用于接收UIGestureRecognizer无法捕获的命令或其他“正常”的应用内交互。你会想要实现接受命令的两种方式。 –

0

将问题发布到Apple支持;谁指出我在正确的方向,需要使用GCMicroGamepad控制器或其相关的GameKit框架。比找到了由blauzahn发布的2015年例子,这个职位绝对值得信任。这里是他的代码稍加修改雨燕3.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...") 
     } 
    } 
}