Now that more and more documentation on the Apple Watch is surfacing has anybody found a way to access and use the device\'s microphone?
I think there are good news from the WWDC 2015 and the new watchOS 2 beta:
Unfortunately, at the moment there is noting about audio in the documentation.
This is inside of an action from a button previously added in your Interface.storyboard, also don't forget to add the Privacy - Microphone Usage Description string in the info.plist.
@IBAction func btnActionRecord() {
var filePath: NSURL!
if let dir: NSString = NSSearchPathForDirectoriesInDomains(.documentDirectory, .allDomainsMask, true).first as NSString? {
let path = dir.appendingPathComponent("myRecord.wav")
filePath = NSURL(string: path)
let audioOptions = [
WKAudioRecorderControllerOptionsActionTitleKey: "Save",
WKAudioRecorderControllerOptionsAlwaysShowActionTitleKey: true,
WKAudioRecorderControllerOptionsAutorecordKey: true,
WKAudioRecorderControllerOptionsMaximumDurationKey: 5.0] as [String : Any]
presentAudioRecorderController(withOutputURL: filePath! as URL, preset: .highQualityAudio, options: audioOptions, completion: {
(didSave, error) -> Void in
if didSave {
print("Audio saved")
let options = [WKMediaPlayerControllerOptionsAutoplayKey: true]
self.presentMediaPlayerController(with: self.filePath as URL, options: options) {
(didPlayToEnd, endTime, error) -> Void in
if didPlayToEnd {
print("Audio finished")
}
if error != nil {
print(error!.self)
}
}
}
if error != nil {
print(error!.localizedDescription)
}
})
}
}
I couldn't find a specific mention of it in the official documentation but on the Developer Forums word from the mods is that it isn't currently possible.
WatchKit currently doesn't provide access to the watch's microphone. You do, however, have access to the iPhone's microphone from the WatchKit extension.
You can access the Watch's microphone on watchOS 2.
1) Create a file URL at which to store the recorded output.
NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,YES);
NSString *path = [[filePaths firstObject] stringByAppendingPathComponent:@"rec.m4a"];
NSURL *fileUrl = [NSURL fileURLWithPath:path];
You may specify the extensions .wav, .mp4, and .m4a.
2) Call the method as follows:
[self presentAudioRecordingControllerWithOutputURL:fileUrl
preset:WKAudioRecordingPresetWideBandSpeech
maximumDuration:5.0
actionTitle:@"Some Title"
completion:^(BOOL didSave, NSError * __nullable error) {
NSLog(@"didSave:%d, error:%@", didSave, error);
}];
You can choose preset in addition to the above
In Swift:
self.presentAudioRecordingControllerWithOutputURL(
self.recFileURL(),
preset: WKAudioRecordingPreset.WideBandSpeech,
maximumDuration: 5.0,
actionTitle: "SomeTitle") { (didSave, error) -> Void in
print("didSave:\(didSave), error:\(error)")
}
You can play the recorded file as follows:
self.presentMediaPlayerControllerWithURL(
fileURL,
options: nil) { (didPlayToEnd, endTime, error) -> Void in
print("didPlayToEnd:\(didPlayToEnd), endTime:\(endTime), error:\(error)")
}
You can check the detail specification here.
If you are looking for dictation, here are some early findings on the SDK: http://natashatherobot.com/watchkit-text-input-dictation-api/
Obviously, cannot be tested until the hardware is out :D
Yes, it is introduced in Watch OS 2.
But as Apple mentioned, this part of the API is in preview, and it did changed a lot. As for Watch OS 2 beta 5, the according interface(in Swift) changed to:
@available(watchOS 2.0, *)
public func presentAudioRecorderControllerWithOutputURL(URL: NSURL, preset: WKAudioRecorderPreset, options: [NSObject : AnyObject]?, completion: (Bool, NSError?) -> Void)
So please always refer to the SDK document you are using, if you want to try this new feature.
By the way, this sample project would be a good start:
https://github.com/shu223/watchOS-2-Sampler
Still, some of the API used in sample is already changed, like this recording one.