是否可以使用OpenEars(或其他软件包)从Apple Watch的麦克风访问语音?
我希望构建一个能够使用手表麦克风收听语音并发现特定关键字的应用。
答案 0 :(得分:1)
不,目前无法直接访问Apple Watch麦克风。我在官方开发者论坛上找到了答案。
WatchKit目前无法访问手表的麦克风。但是,您可以从WatchKit扩展程序访问iPhone的麦克风。
您可以使用Apple提供的听写系统:WatchKit: Speech to text conversion in WatchKit Apps
答案 1 :(得分:1)
从WatchOS 2.1和iOS 9开始,我已经能够以两种不同的方式提出您的建议:
选项1 - 记录WAV文件并上传到ASR服务器 我录制并保存了一个WAV文件到苹果手表。之后我将文件上传到付费语音识别提供商,一切正常!下面是用自己的代码来记录,替换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很快,但是如果你想要做一些高级语音侦察功能(自定义词汇表,语法......),OPTION 1会更方便。我会为大多数用户推荐OPTION 1。 瞧!如果您需要额外的提示,请告诉我!
答案 2 :(得分:0)
我尝试使用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];
}