Recording Audio in Your Watch App - Apple Watch - iOS 9 Swift Programming Cookbook (2015)

iOS 9 Swift Programming Cookbook (2015)

Chapter 2. Apple Watch

2.13 Recording Audio in Your Watch App

Problem

You want to allow your users to record audio while inside your watch app, and you want to get access to the recorded audio.

Solution

Use the presentAudioRecorderControllerWithOutputURL(_:preset:options:completion:) method of your WKInterfaceController class to present a system dialog that can take care of audio recording. If you want to dismiss the dialog, use thedismissAudioRecordingController() method of your controller.

The options parameter of the presentAudioRecorderControllerWithOutputURL(_:preset:options:completion:) method accepts a dictionary that can contain the following keys:

WKAudioRecorderControllerOptionsActionTitleKey

This key, of type String, will be the title of our recorder.

WKAudioRecorderControllerOptionsAlwaysShowActionTitleKey

This key, of type NSNumber, contains a Bool value to dictates whether the title should always be shown on the recorder.

WKAudioRecorderControllerOptionsAutorecordKey

This key, of type NSNumber, contains a Bool value to indicate whether recording should begin automatically when the dialog is presented.

WKAudioRecorderControllerOptionsMaximumDurationKey

This key, of type NSNumber, contains a NSTimeInterval value to dictate the maximum duration of the audio content.

Discussion

For this recipe, we are going to create a watch app whose UI looks like that shown in Figure 2-48). it holds a label to show our current status (started recording, failed recording, etc.) and a button that, upon pressing, can show our recording dialog.

Figure 2-48. Label for status and button

Hook the label up to your code with the name statusLbl. Then hook your record button to your interface under a method named record(). Your interface code should look like this now:

class InterfaceController: WKInterfaceController {

@IBOutlet var statusLbl: WKInterfaceLabel!

...

Define the URL where your recording will be saved:

var url: NSURL{

let fm = NSFileManager()

let url = try! fm.URLForDirectory(NSSearchPathDirectory.MusicDirectory,

inDomain: NSSearchPathDomainMask.UserDomainMask,

appropriateForURL: nil, create: true)

.URLByAppendingPathComponent("recording")

return url

}

Also, because the completion block of our recording screen might not get called on the main thread, create a variable that can set the text inside our status label on the main thread:

var status = ""{

willSet{

dispatch_async(dispatch_get_main_queue()){

self.statusLbl.setText(newValue)

}

}

}

When your record button is pressed, construct your options for the recording:

let oneMinute: NSTimeInterval = 1 * 60

let yes = NSNumber(bool: true)

let no = NSNumber(bool: false)

let options = [

WKAudioRecorderControllerOptionsActionTitleKey : "Audio Recorder",

WKAudioRecorderControllerOptionsAlwaysShowActionTitleKey : yes,

WKAudioRecorderControllerOptionsAutorecordKey : no,

WKAudioRecorderControllerOptionsMaximumDurationKey : oneMinute

]

Last but not least, present your audio recorder to the user and then set the status accordingly:

presentAudioRecorderControllerWithOutputURL(url,

preset: WKAudioRecorderPreset.WideBandSpeech,

options: options){

success, error in

defer{

self.dismissAudioRecorderController()

}

guard success && error == nil else{

self.status = "Failed to record"

return

}

self.status = "Successfully recorded"

}

See Also