我可以使用 OpenEars(或其他)收听和分析来自 Apple Watch 麦克风的语音吗?
Can I use OpenEars (or other) to listen to & analyze speech from the Apple Watch microphone?
是否可以使用 OpenEars(或其他软件包)访问 Apple Watch 麦克风的语音?
我希望构建一个能够收听语音、使用手表麦克风并识别特定关键字的应用。
不,目前无法直接访问 Apple Watch 麦克风。我在官方开发者论坛找到了答案
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.
您可以使用此处说明的 Apple 提供的听写系统:
我尝试使用 AppleWatch Simulator 直接从 AppleWatch 录制音频。
但这只会从 iPhone 模拟器产生声音。您可以对真正的 AppleWatch 做同样的事情,从 iPhone 麦克风录制音频。
- (void) setupRecorder{
[self configureAVAudioSession];
// Set the audio file
NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], // NSUserDomainMask
@"AWAudio.wav",
nil];
file = [NSURL fileURLWithPathComponents:pathComponents];
// Setup audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
// Define the recorder setting
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:16000.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
[recordSetting setValue:[NSNumber numberWithBool:0] forKey:AVLinearPCMIsBigEndianKey];
[recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
// Initiate and prepare the recorder
recorder = [[AVAudioRecorder alloc] initWithURL:file settings:recordSetting error:NULL];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];
}
记录方式示例:
if (player.playing) {
NSLog(@"player playing");
[player stop];
}
if (!recorder.recording) {
NSLog(@"recorder no recording");
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
// Start recording
[recorder record];
[self startAnimation];
[self performSelector:@selector(finishRecordingAfterTwelveSeconds) withObject:nil afterDelay:12.0];
} else {
// Pause recording
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:NO error:nil];
[self stopAnimation];
[self setupFinishRecording];
}
从 WatchOS 2.1 和 iOS9 开始,我已经能够通过两种不同的方式实现您的建议:
选项 1 - 录制 WAV 文件并上传到 ASR 服务器 我录制并保存了一个 WAV 文件到 Apple Watch。之后,我将文件上传到付费语音识别提供商,一切正常!这是要记录的代码,将 UI 更新代码行(和调试代码行)替换为您自己的代码:
//RECORD AUDIO SAMPLE
var saveUrl: NSURL? //this var is initialized in the awakeWithContext method//
func recordAudio(){
let duration = NSTimeInterval(5)
let recordOptions =
[WKAudioRecorderControllerOptionsMaximumDurationKey : duration]
// print("Recording to: "+(saveUrl?.description)!)
//CONSTRUCT AUDIO FILE URL
let fileManager = NSFileManager.defaultManager()
let container = fileManager.containerURLForSecurityApplicationGroupIdentifier("group.artivoice.applewatch");
let fileName = "audio.wav"
saveUrl = container?.URLByAppendingPathComponent(fileName)
presentAudioRecorderControllerWithOutputURL(saveUrl!,
preset: .WideBandSpeech,
options: recordOptions,
completion: { saved, error in
if let err = error {
print(err.description)
self.sendMessageToPhone("Recording error: "+err.description)
}
if saved {
self.btnPlay.setEnabled(true)
self.sendMessageToPhone("Audio was saved successfully.")
print("Audio Saved")
self.uploadAudioSample()
}
})
}
选项 2 - 使用 iWATCH 的母语语音识别 在这种方法中,我采用了原始的本地语音菜单,但是......!我没有添加任何按钮选项,只是纯 ASR。我启动了空的语音菜单,然后恢复了 ASR 返回的字符串。这是代码,享受:
func launchIWatchVoiceRecognition(){
//you can see the empty array [], add options if it suits you well
self.presentTextInputControllerWithSuggestions([], allowedInputMode: WKTextInputMode.Plain, completion:{(results) -> Void in
let aResult = results?[0] as? String
if(!(aResult == nil)){
print(aResult) //print result
self.sendMessageToPhone("Native ASR says: "+aResult!)
dispatch_async(dispatch_get_main_queue()) {
self.txtWatch.setText(aResult) //show result on UI
}
}//end if
})//end show voice menu
}
选项 2 快如闪电,但如果您想执行一些高级语音侦察功能(自定义词汇、语法...),选项 1 会更方便。我会向大多数用户推荐选项 1。 瞧!如果您需要额外的提示,请告诉我!