0

On my watch I have 2 buttons, IBAction playMovie and stopMovie. I have the AVViewController in my iPhone, and movie.mp4 in my phone Images assets. How can I trigger to start and end the movie on AVViewController on the phone target, from the watch button?

I tried player.play() on my watch controller, and this code on the iPhone view controller, but it brings error 'use of unresolved identifier' player on the watch code. Or i call function playmovie() it brings red flag error 'expected declaration'.

iPhone code: 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()

}
Dimitri T
  • 921
  • 1
  • 13
  • 27

1 Answers1

1

You can't segue from a watchOS control to an iOS scene, or have a watchOS action invoke an iOS method or present an iOS view controller.

Your watch code runs on the watch, your iOS code runs on the phone. One app wouldn't be able to call or execute another app's code.

Using Watch Connectivity

You can accomplish what you want by using the WatchConnectivity framework, which allows you to transfer data between your watch app and its paired iOS app.

For example, the watch app could use WCSession sendMessage to send a specific ("playMovie" or "stopMovie") message to the iOS app.

The iOS app WCSessionDelegate didReceiveMessage would handle the specific message received from the watch, and the phone could then locally start or stop playing the movie.

Further information

This SO answer introduces how to setup and start using Watch Connectivity.

For more specifics, refer to this Introducing Watch Connectivity WWDC video, and the Developer Library WCSession Class Reference and WCSessionDelegate Protocol Reference documentation.

Community
  • 1
  • 1
  • Quick Q- so am I right to presume the specific 'play movie' message which I passed be "playmovie()"? I am a beginner so it is helpful to check the precise code. Thanks so much. – Dimitri T Aug 01 '16 at 15:48
  • No, you're not passing a selector or method. You'd be passing a message -- generally a string value within a dictionary such as `["action": "play"]` -- which would mean something specific to your app, and your app would then run the specific code corresponding to the message content. –  Aug 01 '16 at 15:53
  • ah. So I send the message, and then I add a function 'if action = play' (then) 'playmovie() ?' – Dimitri T Aug 02 '16 at 12:19
  • You don't add a function. You handle the message in `didReceiveMessage`, check to see if it is "play", then call `playMovie()`. It would be `if action == "play"`. (You're not making an assignment, you're comparing two strings.) If you need help with this, I recommend [following this tutorial](http://www.techotopia.com/index.php/A_watchOS_2_WatchConnectivity_Messaging_Tutorial) that shows exactly how to control playback on a phone from a watch. See "Sending the message to the iOS app," and "Handling the message in the iOS app" for an example of what I suggested. –  Aug 02 '16 at 13:13