首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >让MPRemoteCommandCenter.shared()在tvOS中工作

让MPRemoteCommandCenter.shared()在tvOS中工作
EN

Stack Overflow用户
提问于 2017-06-15 08:48:48
回答 2查看 948关注 0票数 1

好吧,也许我漏掉了什么。我想用我的应用程序使用黑色遥控器,并从WWDC 2017关于这个问题的谈话中获得了这段代码。上面写着..。

对媒体播放的一致和直观的控制是tvOS上许多应用程序的关键,正确使用和配置MPNowPlayingInfoCenter和MPRemoteCommandCenter对于提供良好的用户体验至关重要。深入研究这些框架,了解如何确保您的应用程序是否使用Siri、Siri远程或iOS远程应用进行控制。

所以我在我的viewDidLoad of tvOS应用程序中添加了这些行,它们基本上什么也不做?

代码语言:javascript
复制
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,这项工作,还是我在某个地方遗漏了要点?

代码语言:javascript
复制
<key>UIBackgroundModes</key>
<array>
    <string>audio</string>
    <string>external-accessory</string>
</array>
EN

回答 2

Stack Overflow用户

发布于 2017-06-15 15:59:54

当应用程序处于前台时,MPRemoteCommandCenter中的命令不是由Siri遥控器触发的。若要在前台从远程获取事件,请按照您可能已经习惯的方式使用UIGestureRecognizer

MPRemoteCommandCenter中的这些命令用于系统可能希望与您的播放交互的其他方式,例如:

  • 你的应用程序在后台播放音频,用户按下遥控器上的暂停按钮:你的应用程序我会被要求暂停播放。
  • 用户正在为iOS使用电视远程应用程序,并使用该应用程序的播放控制屏幕。
票数 1
EN

Stack Overflow用户

发布于 2017-06-24 09:52:08

向苹果支持部门发布了这个问题;谁为我指明了正确的方向,需要使用GCMicroGamepad控制器或相关的GameKit框架。比找到了一个2015年的例子布劳赞恩,谁最肯定值得赞扬,真正的这篇文章。以下是他为SWIFT3.0、ios 10.x所做的稍微修改的代码

代码语言:javascript
复制
import GameController

。。

代码语言:javascript
复制
 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...")
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44562925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档