我知道Google云语音API(识别)被限制为65秒,但是我认为这是每个请求的限制,并且只有在您的发言时间长于该限制时才受限制。
因此,假设我只想从麦克风翻译一些命令并向Google语音请求。我添加模型command_and_search并将setSingleUtterance(true)设置为config。
下面的代码发送一个请求并打印结果,但是我不明白如何为下一个命令生成新请求。如“帮助” ....“正确” ...等
import com.google.api.gax.rpc.ClientStream;
import com.google.api.gax.rpc.ResponseObserver;
import com.google.api.gax.rpc.StreamController;
import com.google.cloud.speech.v1p1beta1.*;
import com.google.protobuf.ByteString;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.TargetDataLine;
public class App {
public static void AskGoogle (ClientStream<StreamingRecognizeRequest> c,byte[] b ) {
StreamingRecognizeRequest request =
StreamingRecognizeRequest.newBuilder()
.setAudioContent(ByteString.copyFrom(b))
.build();
c.send(request);
return;
}
public static void main(String[] args) {
// Target data line
TargetDataLine line = null;
AudioInputStream audio = null;
// Capture Microphone Audio Data
try {
// Signed PCM AudioFormat with 16kHz, 16 bit sample size, mono
int sampleRate = 16000;
AudioFormat format = new AudioFormat(sampleRate, 16, 1, true, false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
// Check if Microphone is Supported
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Line not supported");
System.exit(0);
}
// Get the target data line
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);
line.start();
// Audio Input Stream
audio = new AudioInputStream(line);
} catch (Exception ex) {
ex.printStackTrace();
}
// Send audio from Microphone to Google Servers and return Text
try (SpeechClient client = SpeechClient.create()) {
ResponseObserver<StreamingRecognizeResponse> responseObserver =
new ResponseObserver<StreamingRecognizeResponse>() {
public void onStart(StreamController controller) {
// do nothing
}
public void onResponse(StreamingRecognizeResponse response) {
System.out.println(response);
}
public void onComplete() {
}
public void onError(Throwable t) {
System.out.println(t);
}
};
ClientStream<StreamingRecognizeRequest> clientStream =
client.streamingRecognizeCallable().splitCall(responseObserver);
RecognitionConfig recConfig =
RecognitionConfig.newBuilder()
.setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
.setLanguageCode("sv-SE")
.setSampleRateHertz(16000)
.setModel("command_and_search")
.build();
StreamingRecognitionConfig config =
StreamingRecognitionConfig.newBuilder().setConfig(recConfig).setSingleUtterance(true).build();
StreamingRecognizeRequest request =
StreamingRecognizeRequest.newBuilder()
.setStreamingConfig(config)
.build(); // The first request in a streaming call has to be a config
clientStream.send(request);
while (true) {
byte[] data = new byte[100];
try {
audio.read(data);
} catch (IOException e) {
System.out.println(e);
}
AskGoogle(clientStream,data);
}
} catch (Exception e) {
System.out.println(e);
}
}
}
答案 0 :(得分:2)
无论出于何种原因,这个问题都被否决了,我实际上找到了解决我的问题的方法,可以在OnResponse方法(上面的代码)返回第一次转录后继续向语音api发送更多请求。只需将尝试捕获麦克风块中的所有内容包装起来,以使用语音客户端请求新配置,将其转换为上述AskGoogle()之类的方法,并在OnResponse的StreamingRecognizeResponse中的endpointerType“ END_OF_UTTERANCE”出现时再次调用它即可。