如何从 swift 2.2 中的观看按钮在 phone AV 控制器上启动电影?

how to start movie on phone AV controller from watch button in swift 2.2?

我的手表上有 2 个按钮,IBAction playMovie 和 stopMovie。我的 iPhone 中有 AVViewController,phone 图片资产中有 movie.mp4。如何从观看按钮触发在 phone 目标上的 AVViewController 上开始和结束电影?

我在我的手表控制器上尝试了 player.play(),在 iPhone 视图控制器上尝试了此代码,但它在手表代码上带来了错误 'use of unresolved identifier' 播放器。或者我调用函数 playmovie() 它会带来红旗错误 'expected declaration'.

iPhone代码: func playmovie(){

    let videoURL = NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource("video1bunny", ofType:"mp4")!)
    let player = AVPlayer(URL: videoURL)
    let playerViewController = AVPlayerViewController()
    playerViewController.player = player
    presentViewController(playerViewController, animated: true) { () -> Void in
        player.play()

}

您不能从 watchOS 控件转到 iOS 场景,或者让 watchOS 操作调用 iOS 方法或呈现 iOS 视图控制器。

你的手表代码在手表上运行,你的iOS代码在phone上运行。一个应用程序将无法调用或执行另一个应用程序的代码。

使用手表连接

可以 通过使用 WatchConnectivity 框架完成您想要的事情,该框架允许您在手表应用程序及其配对的 iOS 应用程序之间传输数据.

例如,手表应用程序可以使用 WCSession sendMessage 向 iOS 应用程序发送特定("playMovie" 或 "stopMovie")消息。

iOS 应用程序 WCSessionDelegate didReceiveMessage 将处理从手表收到的特定消息,然后 phone 可以在本地开始或停止播放电影。

更多信息

介绍如何设置和开始使用手表连接。

有关详细信息,请参阅此 Introducing Watch Connectivity WWDC video, and the Developer Library WCSession Class Reference and WCSessionDelegate Protocol Reference 文档。