Access Apple Watch's microphone

2019-01-14 03:30发布

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?

标签: watchkit
5条回答
做自己的国王
2楼-- · 2019-01-14 03:54

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

  • WKAudioRecordingPresetNarrowBandSpeech
  • WKAudioRecordingPresetHighQualityAudio

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.

查看更多
姐就是有狂的资本
3楼-- · 2019-01-14 03:55

I think there are good news from the WWDC 2015 and the new watchOS 2 beta:

WWDC watchOS

Unfortunately, at the moment there is noting about audio in the documentation.

查看更多
太酷不给撩
4楼-- · 2019-01-14 03:56

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.

查看更多
Ridiculous、
5楼-- · 2019-01-14 03:56

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.

查看更多
Anthone
6楼-- · 2019-01-14 04:15

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

查看更多
登录 后发表回答