我正在尝试基于使用SpeechGrpc api的android应用示例(https://github.com/GoogleCloudPlatform/android-docs-samples/tree/master/speech/Speech)来执行longRunningRecognize,代码正在无异常运行,大约一分钟后,我得到了响应,但我得到了替代方法结果为空,尝试了几个文件,我将很高兴了解我在做什么错? 代码:
private SpeechGrpc.SpeechStub mApi;
private OperationsGrpc.OperationsStub operationApi;
public void recognizeLongGcs(String gcsUri) throws Exception {
try {
mApi.longRunningRecognize(LongRunningRecognizeRequest.newBuilder()
.setConfig(
RecognitionConfig.newBuilder()
.setEncoding(RecognitionConfig.AudioEncoding.FLAC)
.setLanguageCode("iw")
.setSampleRateHertz(16000)
.build())
.setAudio( RecognitionAudio.newBuilder().setUri(gcsUri).build())
.build(),
mLongFileResponseObserver);
}
catch (Exception e)
{ AnalyticsModel.getInstance().logError(this.getClass().getSimpleName() , e );
}
}
private final StreamObserver<Operation> mLongFileResponseObserver
= new StreamObserver<Operation>() {
Operation response;
String responseName;
@Override
public void onNext(Operation response) {
String text = null;
List<WordInfo> words = null;
responseName =response.getName();
try {
if (response.getDone())
{
Any a = response.getResponse();
LongRunningRecognizeResponse longRunningRecognizeResponse = LongRunningRecognizeResponse.parseFrom(a.getValue());
final SpeechRecognitionResult result = longRunningRecognizeResponse.getResults(0);
if (result.getAlternativesCount() > 0) {
final SpeechRecognitionAlternative alternative = result.getAlternatives(0);
text = alternative.getTranscript();
words = alternative.getWordsList();
}
}
else {
Thread.sleep(10000);
operationApi.getOperation(GetOperationRequest.newBuilder().setName(responseName).build(), mLongFileResponseObserver);
}
}
catch (Exception e) { }
}
@Override
public void onError(Throwable t) {
Log.e(TAG, "Error calling the API.", t);
}
@Override
public void onCompleted() {
Log.i(TAG, "API completed.");
}
};