gpt4 book ai didi

java - 如何在网络上使用谷歌语音转文本

转载 作者:行者123 更新时间:2023-12-02 04:38:21 27 4
gpt4 key购买 nike

我想创建语音转文本网页,例如使用 Google 语音转文本在网络上运行的 Google 语音转文本示例。

Google 语音转文本示例:https://cloud.google.com/speech-to-text/?hl=en

我尝试使用Google提供的示例代码。此代码在 localhost 上运行良好,但在 ibm cloud 中出现以下错误:

无行匹配接口(interface) TargetDataLine 支持格式 PCM_SIGNED 16000.0 Hz、16 位、单声道、2 字节/帧、little-endian。

我不知道如何解决这个错误。

ResponseObserver<StreamingRecognizeResponse> responseObserver = null;

try (SpeechClient client = SpeechClient.create()) {

responseObserver =
new ResponseObserver<StreamingRecognizeResponse>() {
ArrayList<StreamingRecognizeResponse> responses = new ArrayList<>();

public void onStart(StreamController controller) {}

public void onResponse(StreamingRecognizeResponse response) {
responses.add(response);
}

public void onComplete() {
for (StreamingRecognizeResponse response : responses) {
StreamingRecognitionResult result = response.getResultsList().get(0);
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
System.out.printf("Transcript : %s\n", alternative.getTranscript());
}
}

public void onError(Throwable t) {
System.out.println(t);
}
};

ClientStream<StreamingRecognizeRequest> clientStream =
client.streamingRecognizeCallable().splitCall(responseObserver);

RecognitionConfig recognitionConfig =
RecognitionConfig.newBuilder()
.setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
.setLanguageCode("en-US")
.setSampleRateHertz(16000)
.build();
StreamingRecognitionConfig streamingRecognitionConfig =
StreamingRecognitionConfig.newBuilder().setConfig(recognitionConfig).build();

StreamingRecognizeRequest request =
StreamingRecognizeRequest.newBuilder()
.setStreamingConfig(streamingRecognitionConfig)
.build(); // The first request in a streaming call has to be a config

clientStream.send(request);
// SampleRate:16000Hz, SampleSizeInBits: 16, Number of channels: 1, Signed: true,
// bigEndian: false
AudioFormat audioFormat = new AudioFormat(16000, 16, 1, true, false);
DataLine.Info targetInfo =
new Info(
TargetDataLine.class,
audioFormat); // Set the system information to read from the microphone audio stream

if (!AudioSystem.isLineSupported(targetInfo)) {
System.out.println("Microphone not supported");
System.exit(0);
}
// Target data line captures the audio stream the microphone produces.
TargetDataLine targetDataLine = (TargetDataLine) AudioSystem.getLine(targetInfo);
targetDataLine.open(audioFormat);
targetDataLine.start();
System.out.println("Start speaking");
long startTime = System.currentTimeMillis();
// Audio Input Stream
AudioInputStream audio = new AudioInputStream(targetDataLine);
while (true) {
long estimatedTime = System.currentTimeMillis() - startTime;
byte[] data = new byte[6400];
audio.read(data);
if (estimatedTime > 60000) { // 60 seconds
System.out.println("Stop speaking.");
targetDataLine.stop();
targetDataLine.close();
break;
}
request =
StreamingRecognizeRequest.newBuilder()
.setAudioContent(ByteString.copyFrom(data))
.build();
clientStream.send(request);
}
} catch (Exception e) {
System.out.println(e);
}
responseObserver.onComplete();

最佳答案

您是否已将所有需要的访问 key 导入到您的系统环境中?Google 不允许在没有 API key 的情况下调用他们的 API。您可以在此处找到如何将 API 添加到环境中 https://cloud.google.com/docs/authentication/api-keys

关于java - 如何在网络上使用谷歌语音转文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56537193/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com