1

我正在使用以下 java 代码来调用 Google Cloud Speech api,但我没有收到任何响应,有时还会收到 DEADLINE_EXCEEDED 消息

注意:这在家庭 wi-fi 中工作,仅在公司网络中我看到这个问题。非常感谢任何指针

如果我遗漏了什么,请告诉我。另外,对于身份验证部分,我如何验证它是否经过身份验证?

   FileInputStream credentialsStream = new FileInputStream("C:/service-account-file.json");
    GoogleCredentials credentials = GoogleCredentials.fromStream(credentialsStream);
    FixedCredentialsProvider credentialsProvider = FixedCredentialsProvider.create(credentials);

    SpeechSettings speechSettings = 
            SpeechSettings.newBuilder()
                .setCredentialsProvider(credentialsProvider)
                .build();       


    // Instantiates a client

    SpeechClient speechClient = SpeechClient.create(speechSettings);    


        RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC;
            int sampleRateHertz = 16000;
            String languageCode = "en-US";
            RecognitionConfig config = RecognitionConfig.newBuilder()
              .setEncoding(encoding)
              .setSampleRateHertz(sampleRateHertz)
              .setLanguageCode(languageCode)
              .build();

             String fileName = "C:\\Hello.flac";

             Path path = Paths.get(fileName);
              byte[] data = Files.readAllBytes(path);
              ByteString audioBytes = ByteString.copyFrom(data);

              RecognitionAudio audio = RecognitionAudio.newBuilder()
                .setContent(audioBytes)
                .build();


             RecognizeResponse response = speechClient.recognize(config, audio);


            List<SpeechRecognitionResult> results = response.getResultsList();

            for (SpeechRecognitionResult result: results) {
              // There can be several alternative transcripts for a given chunk of speech. Just use the
              // first (most likely) one here.
              SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
              System.out.printf("Transcription: %s%n", alternative.getTranscript());
            }
            speechClient.close();
    }catch(Exception e){
        e.printStackTrace();
    }

错误堆栈跟踪:-

com.google.api.gax.rpc.DeadlineExceededException: io.grpc.StatusRuntimeException: DEADLINE_EXCEEDED
    at com.google.api.gax.rpc.ApiExceptionFactory.createException(ApiExceptionFactory.java:51)
    at com.google.api.gax.grpc.GrpcExceptionCallable$ExceptionTransformingFuture.setException(GrpcExceptionCallable.java:118)
    at com.google.api.gax.grpc.GrpcExceptionCallable$ExceptionTransformingFuture.onFailure(GrpcExceptionCallable.java:101)
    at com.google.api.core.ApiFutures$1.onFailure(ApiFutures.java:53)
    at com.google.common.util.concurrent.Futures$4.run(Futures.java:1126)
    at com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:399)
    at com.google.common.util.concurrent.AbstractFuture.executeListener(AbstractFuture.java:902)
    at com.google.common.util.concurrent.AbstractFuture.complete(AbstractFuture.java:813)
    at com.google.common.util.concurrent.AbstractFuture.setException(AbstractFuture.java:677)
    at io.grpc.stub.ClientCalls$GrpcFuture.setException(ClientCalls.java:463)
    at io.grpc.stub.ClientCalls$UnaryStreamToFuture.onClose(ClientCalls.java:439)
    at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:428)
    at io.grpc.internal.ClientCallImpl.access$100(ClientCallImpl.java:76)
    at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.close(ClientCallImpl.java:514)
    at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.access$700(ClientCallImpl.java:431)
    at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:546)
    at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:52)
    at io.grpc.internal.SerializingExecutor$TaskRunner.run(SerializingExecutor.java:152)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:522)
    at java.util.concurrent.FutureTask.run(FutureTask.java:277)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:191)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1153)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.lang.Thread.run(Thread.java:785)
Caused by: io.grpc.StatusRuntimeException: DEADLINE_EXCEEDED
    at io.grpc.Status.asRuntimeException(Status.java:540)
    ... 15 more
4

0 回答 0