我正在尝试使用Google云语音API。我发现了一个奇怪的行为,发生了好几次。我不明白为什么,我无法从谷歌云语音得到任何回应。
以下是代码:
public String ConvertSpeech(File audioFile) {
SpeechClient speech = null;
try {
speech = SpeechClient.create();
} catch (IOException e) {
e.printStackTrace();
}
Path path = Paths.get(audioFile.getName());
byte[] data = null;
try {
data = Files.readAllBytes(path);
} catch (IOException e) {
e.printStackTrace();
}
ByteString audioBytes = ByteString.copyFrom(data);
// Configure request with local raw PCM audio
RecognitionConfig config = RecognitionConfig.newBuilder()
.setEncoding(AudioEncoding.LINEAR16)
.setLanguageCode("fr-FR")
.setSampleRateHertz(16000)
.build();
RecognitionAudio audio = RecognitionAudio.newBuilder()
.setContent(audioBytes)
.build();
// Use blocking call to get audio transcript
// RecognizeResponse response = speech.recognize(config, audio);
OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> response =
speech.longRunningRecognizeAsync(config, audio);
while (!response.isDone()) {
System.out.println("Waiting for response...");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
List<SpeechRecognitionResult> results = new ArrayList<>();
try {
results = response.get().getResultsList();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ExecutionException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// List<SpeechRecognitionResult> results = response.getResultsList();
// There can be several alternative transcripts for a given chunk of speech. Just use the
// first (most likely) one here.
if(results.size()!=0){
SpeechRecognitionAlternative alternative = results.get(0).getAlternativesList().get(0);
// System.out.printf("Transcription: %s%n", alternative.getTranscript());
try {
speech.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return alternative.getTranscript();
}
// System.out.printf("Transcription: %s%n", alternative.getTranscript());
try {
speech.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
当我运行时,一切都还可以,但有时候,谷歌会在当天剩下的时间里停止回答。然后,tomorow早上一切都很好。它不像api限制,因为我只在错误发生前使用它几次(大约4次5秒)。
首先我认为这是因为格式错误,但我将所有声音文件存储为.wav的方式相同,因此没有理由存在某些错误,而其他错误也没有。
例如,今天已经发送了5个请求,第6个,第7个和第8个错误,并且在谷歌云控制台上,我无法找到有关这些失败请求的任何信息。看起来这些请求在
之间消失了OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata>
response = speech.longRunningRecognizeAsync(config, audio);
和谷歌云演讲。
这是控制台输出:
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
Waiting for response...
java.util.concurrent.ExecutionException: com.google.api.gax.rpc.DeadlineExceededException: io.grpc.StatusRuntimeException: DEADLINE_EXCEEDED: deadline exceeded after 599979147331ns
at com.google.common.util.concurrent.AbstractFuture.getDoneValue(AbstractFuture.java:500)
at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:459)
at com.google.api.gax.longrunning.OperationFutureImpl.get(OperationFutureImpl.java:106)
at mypackage.GoogleCloudSpeechService.ConvertSpeech(GoogleCloudSpeechService.java:66)