Playing Local and Remote Audio and Video in Your Watch App - Apple Watch - iOS 9 Swift Programming Cookbook (2015)

iOS 9 Swift Programming Cookbook (2015)

Chapter 2. Apple Watch

2.14 Playing Local and Remote Audio and Video in Your Watch App

Problem

You want to play audio or video files, whether they are saved locally or online.

Solution

Use the presentMediaPlayerControllerWithURL(_:options:completion:) instance method of your interface controller (WKInterfaceController). Close the media player with the dismissMediaPlayerController() method.

Discussion

The first parameter to this method is just the URL from which the media must be loaded. The options parameter is a dictionary that can have the following keys:

WKMediaPlayerControllerOptionsAutoplayKey

A boolean value (wrapped inside an NSNumber instance) that dictates whether the media should autoplay when it is opened. This is set to false by default.

WKMediaPlayerControllerOptionsStartTimeKey

The number of seconds (of type NSTimeInterval) into the media where you want to start it.

WKMediaPlayerControllerOptionsVideoGravityKey

A value of type WKVideoGravity (place its raw integer value in your dictionary) that dictates the scaling of the video. You can, for instance, specify WKVideoGravity.ResizeAspectFill.

WKMediaPlayerControllerOptionsLoopsKey

A boolean value (wrapped inside NSNumber) that specifies whether the media has to loop automatically. The default is false.

For this recipe, we are going to create a UI similar to that in Recipe 2.13 (see Figure 2-48). Our UI looks like Figure 2-49.

Figure 2-49. Label to show the current status, and a button to start the playback

Hook up the label to an outlet called statusLbl and the action of the button to a method called play(). Then create a variable in your code called status of type String, just as we did in Recipe 2.13. In the play method, first construct your URL:

guard let url = NSURL(string: "http://localhost:8888/video.mp4") else{

status = "Could not create url"

return

}

NOTE

I am running MAMP (free version) on my computer and I’m hosting a video called video.mp4. You can download lots of public domain files by just searching online.

Now construct your options dictionary. I want the media player to do the followings:

§ Auto play my video

§ Loop the video

§ Resize the video so that it fills the entire screen

§ Start at 4 seconds into the video

let gravity = WKVideoGravity.ResizeAspectFill.rawValue

let options = [

WKMediaPlayerControllerOptionsAutoplayKey : NSNumber(bool: true),

WKMediaPlayerControllerOptionsStartTimeKey : 4.0 as NSTimeInterval,

WKMediaPlayerControllerOptionsVideoGravityKey : gravity,

WKMediaPlayerControllerOptionsLoopsKey : NSNumber(bool: true),

]

Now start the media player and handle any possible errors:

presentMediaPlayerControllerWithURL(url, options: options) {

didPlayToEnd, endTime, error in

self.dismissMediaPlayerController()

guard error == nil else{

self.status = "Error occurred \(error)"

return

}

if didPlayToEnd{

self.status = "Played to end of the file"

} else {

self.status = "Did not play to end of file. End time = \(endTime)"

}

}

See Also