Okay, we've got the visuals, let's make some noise!
It has to be said that audio functionality on the Apple Watch is very limited. It is yet to be seen how the platform develops in this respect, but, as things stand, there is not a great deal you can do with media at the moment beyond simply playing an audio or video file. Perhaps the rationale behind this is that a device that beeps and squeaks at every opportunity is likely to be an irritation not just to the user who is wearing it, but to everybody else in the near vicinity as well. Everything that was said about animation applies to audio tenfold and is probably considered less of a must-have than an if-you-must. And, of course, there is no headphone socket to relieve your family members of this new source of background noise.
However, that said, if we keep an open mind on the subject, we do have an opportunity to explore the extent to which we can use audio on the Apple Watch, and it is likely that there will be a hundred hitherto unimagined use cases just waiting to become as mainstream as puppy videos on an iPhone are today (and believe me, nobody was thinking of that
in the 1990's).
There are a few approaches to getting audio to play on the watch and we will employ the simplest here. This involves employing the WKMediaPlayerController
, which is provided by WatchKit. This player is also capable of playing video, so although we will be using a very small audio file in this example, there is no additional code necessary to play an .mp4 video file.
There is a small audio file available in the GitHub repo for this book if you don't have any media to hand:
https://github.com/codingTheHole/BuildingAppleWatchProjectsBook
We will use it for this chapter's example, since such a small file will load very quickly and, further, will not bloat the size of the app.
When we say media file, we are referring to any of the formats that can be played by the WKMediaPlayerController
, which include the .aiff
format we will use here and the .mp4
video format.
To add a media file to your project, simply drag it from the finder into the project navigator and select Copy items if needed, ensuring that the WatchKit Extension target is selected, as illustrated here:
Now that we have added the media file to the WatchKit Extension's bundle, let's write a function that we can call from anywhere in our code to present the system's own media player, loaded with that file.
Add the following code to whichever InterfaceController
object is required to present the player. If you still have our AnimationInterfaceController
class open, you could use that:
func playSound() { if let assetUrl = NSBundle.mainBundle().URLForResource( //1 "donk", withExtension: "aiff") { let options = //2 [WKMediaPlayerControllerOptionsAutoplayKey :"true"] presentMediaPlayerControllerWithURL( //3 assetUrl, options: options, completion: { didPlayToEnd, endTime, error in //4 // completion code here, if any }) } }
The comments in the code are as follows:
mainBundle
. Careful with the typing, any mistakes here will mean that the asset (that is, the file) doesn't load, but there will be no indication that an error has occurred.WKMediaPlayerControllerOptionsAutoplayKey
to true
. As you can imagine, this means that the file will play as soon as it is loaded.presentMediaPlayerControllerWithURL
, passing it the assetUrl
and options
we just created and a completion
closure, which the system will call, supplying a Bool
value that tells us whether or not the file played to the end, an NSTimeInterval
informing us how much of the media was played, and an NSError
if anything went wrong.You must now call this method from somewhere in your code. At the moment we have a red button that doesn't do much, so let's add the call to its IBAction
, buttonBTapped
:
@IBAction func buttonBTapped() {
print("Entering the Matrix?")
playSound()
}
Now when you select red from the two buttons, you will not only see the enigmatic Entering the Matrix?
message in the console, you will also be presented with the watchOS media player, containing possibly the most inappropriate sound ever to accompany an initiation into the dark and mysterious world of Morpheus.
You might like to replace the media player with an exciting game instead.
13.58.148.186