【发布时间】:2018-03-02 09:40:03
【问题描述】:
我正在开发 Bot 应用程序,这里我有 2 个功能
- 语音转文字
- 文字转语音
两者都按预期工作,但我想检测到当用户停止说话时我想停止检测并将该数据发送到服务器。
有什么方法可以让用户不说话?
我正在使用以下代码进行语音检测:
// Starts an AVAudio Session
NSError *error;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
[audioSession setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];
// Starts a recognition process, in the block it logs the input or stops the audio
// process if there's an error.
recognitionRequest = [[SFSpeechAudioBufferRecognitionRequest alloc] init];
AVAudioInputNode *inputNode = audioEngine.inputNode;
recognitionRequest.shouldReportPartialResults = YES;
recognitionTask = [speechRecognizer recognitionTaskWithRequest:recognitionRequest resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
BOOL isFinal = NO;
if (result) {
// Whatever you say in the microphone after pressing the button should be being logged
// in the console.
NSLog(@"RESULT:%@",result.bestTranscription.formattedString);
self.inputToolbar.contentView.textView.text = result.bestTranscription.formattedString;
self.inputToolbar.contentView.rightBarButtonItem.enabled = YES;
isFinal = !result.isFinal;
}
if (error) {
if (audioEngine != NULL) {
[audioEngine stop];
[inputNode removeTapOnBus:0];
recognitionRequest = nil;
recognitionTask = nil;
}
}
}];
// Sets the recording format
AVAudioFormat *recordingFormat = [inputNode outputFormatForBus:0]; //[[AVAudioFormat alloc] initStandardFormatWithSampleRate:44100 channels:1];
[inputNode installTapOnBus:0 bufferSize:1024 format:recordingFormat block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {
[recognitionRequest appendAudioPCMBuffer:buffer];
}];
// Starts the audio engine, i.e. it starts listening.
[audioEngine prepare];
[audioEngine startAndReturnError:&error];
NSLog(@"Say Something, I'm listening");
如果有人需要更多详细信息,请告诉我。
提前致谢。
【问题讨论】:
-
您如何使用 Pushpendra 获得解决方案。 @CodeChanger
标签: ios objective-c speech-recognition speech-to-text