TextToSpeechApi中的抽象方法“ com.google.api.gax.tracing.ApiTracer”错误,如何解决?

时间:2019-03-26 09:47:14

标签: android google-text-to-speech abstractmethoderror

我正在使用TextToSpeechApi来阅读具有多种语言和声音的文本。 但它显示java.lang.AbstractMethodError:上的SynthesizeSpeechResponse错误 线。如何解决这个问题。

我的礼物:

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'

implementation 'com.google.cloud:google-cloud-translate:1.65.0'
// add these dependencies for the speech client
implementation 'io.grpc:grpc-okhttp:1.10.0'
implementation 'com.google.cloud:google-cloud-speech:0.53.0-alpha'
implementation 'com.google.cloud:google-cloud-texttospeech:0.53.0-beta'

implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
}

我的Java编码:

public class TextReadCloudApi extends AppCompatActivity {

private static final String TAG = "TextReadCloudApi";
private TextToSpeechClient textToSpeechClient;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.text_read_api);

    Button button = (Button) findViewById(R.id.readBtn);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Speak();
        }
    });
}

private void Speak() {
    try {
        final InputStream stream = getResources().openRawResource(R.raw.transcribeapi);
        ServiceAccountCredentials credentials = ServiceAccountCredentials.fromStream(stream);
        TextToSpeechSettings settings = TextToSpeechSettings.newBuilder()
                .setCredentialsProvider(FixedCredentialsProvider.create(credentials)).build();
        this.textToSpeechClient = TextToSpeechClient.create(settings);
        SetUp();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@SuppressLint("StaticFieldLeak")
private void SetUp() {

    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            // Set the text input to be synthesized
            SynthesisInput input = SynthesisInput.newBuilder()
                    .setText("Hello, World!")
                    .build();

            // Build the voice request, select the language code ("en-US") and the ssml voice gender
            // ("neutral")
            VoiceSelectionParams voice = VoiceSelectionParams.newBuilder()
                    .setLanguageCode("en")
                    .setSsmlGender(SsmlVoiceGender.FEMALE)
                    .build();


            // Select the type of audio file you want returned
            AudioConfig audioConfig = AudioConfig.newBuilder()
                    .setAudioEncoding(AudioEncoding.MP3)
                    .build();

            SynthesizeSpeechRequest speechRequest=SynthesizeSpeechRequest.newBuilder()
                    .setVoice(voice)
                    .setAudioConfig(audioConfig)
                    .setInput(input)
                    .build();

            // Perform the text-to-speech request on the text input with the selected voice parameters and
            // audio file type
            if(!textToSpeechClient.isShutdown()) {
          SynthesizeSpeechResponse response=textToSpeechClient.synthesizeSpeech(speechRequest);
//            SynthesizeSpeechResponse response=textToSpeechClient.synthesizeSpeechCallable().call(speechRequest);
//            SynthesizeSpeechResponse response = textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);

                // Get the audio contents from the response
                ByteString audioContents = response.getAudioContent();

                // Write the response to the output file.
                try {
                    OutputStream out = new FileOutputStream("output.mp3");
                    out.write(audioContents.toByteArray());
                    System.out.println("Audio content written to file \"output.mp3\"");
                    Log.d(TAG, "SetUp: Audio content written to file");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    }.execute();

}
}

错误:

 Caused by: java.lang.AbstractMethodError: abstract method "com.google.api.gax.tracing.ApiTracer com.google.api.gax.rpc.ApiCallContext.getTracer()"
    at com.google.api.gax.retrying.BasicRetryingFuture.handleAttempt(BasicRetryingFuture.java:141)
    at com.google.api.gax.retrying.CallbackChainRetryingFuture$AttemptCompletionListener.handle(CallbackChainRetryingFuture.java:135)
    at com.google.api.gax.retrying.CallbackChainRetryingFuture$AttemptCompletionListener.run(CallbackChainRetryingFuture.java:117)
    at com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:398)
    at com.google.common.util.concurrent.AbstractFuture.executeListener(AbstractFuture.java:1030)
    at com.google.common.util.concurrent.AbstractFuture.addListener(AbstractFuture.java:675)
    at com.google.common.util.concurrent.AbstractFuture$TrustedFuture.addListener(AbstractFuture.java:105)
    at com.google.common.util.concurrent.ForwardingListenableFuture.addListener(ForwardingListenableFuture.java:45)
    at com.google.api.gax.retrying.CallbackChainRetryingFuture.setAttemptFuture(CallbackChainRetryingFuture.java:93)
    at com.google.api.gax.rpc.AttemptCallable.call(AttemptCallable.java:89)
    at com.google.api.gax.rpc.RetryingCallable.futureCall(RetryingCallable.java:63)
    at com.google.api.gax.rpc.RetryingCallable.futureCall(RetryingCallable.java:41)
    at com.google.api.gax.rpc.UnaryCallable$1.futureCall(UnaryCallable.java:126)
    at com.google.api.gax.rpc.UnaryCallable.futureCall(UnaryCallable.java:87)
    at com.google.api.gax.rpc.UnaryCallable.call(UnaryCallable.java:112)
    at com.google.cloud.texttospeech.v1.TextToSpeechClient.synthesizeSpeech(TextToSpeechClient.java:270)
    at com.logicvalley.translator.sample.TextReadCloudApi$2.doInBackground(TextReadCloudApi.java:97)
    at com.logicvalley.translator.sample.TextReadCloudApi$2.doInBackground(TextReadCloudApi.java:67)
    at android.os.AsyncTask$2.call(AsyncTask.java:333)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
    at java.lang.Thread.run(Thread.java:764) 
 8322

请任何人给我解决方案,

提前谢谢...

0 个答案:

没有答案