如何检查tts是否已完成发言?

时间:2018-05-14 12:51:50

标签: java android

我已经创建了一个Text To Speech Android应用程序。我想将按钮文字从发言更改为停止,然后在完成发言后再次更改为发言

它有两个按钮 - 清除所有发言发言应在点击时更改为停止,并在发言结束后再次更改为发言

Activity screenshot (UI)

我在StackOverflow和网上搜索了很多,包括他们的官方文档,但无法找到适合我程序的解决方案。

请帮助我,我是新手。 THANKYOU!

  //text to speech

    t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {

            if (status != TextToSpeech.ERROR)
                t1.setLanguage(Locale.UK);

                }
    });




    //onClick
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak = ed1.getText().toString();
            if(flag==0) { //flag variable to check state and to change button text
                if(toSpeak=="")
                {
                    Toast.makeText(getApplicationContext(), "Empty, enter something!", Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(getApplicationContext(), "Speaking", Toast.LENGTH_SHORT).show();
                    t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
                    flag = 1;
                    b1.setText("Stop");
                    flag = 1;
                }
            }
            else {

                flag=0;
                String te=ed1.getText().toString();
                ed1.setText("");
                toSpeak = ed1.getText().toString();
                ed1.setText(te);
                t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
                b1.setText("Play");
                Toast.makeText(getApplicationContext(), "Stopped", Toast.LENGTH_SHORT).show();




            }

        }
    });


//clear all button. clears the text field and changes text on the button1 to play
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
        public void onClick(View view) {

                ed1.setText("");
                b1.setText("Play");flag=0;
        }
        });

3 个答案:

答案 0 :(得分:1)

请勿使用'TextToSpeech.OnUtteranceCompletedListener'使用以下内容:

  @Override
  public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
    yourTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
        @Override
        public void onDone(String utteranceId) {
            // Log.d("YourActivity", "TtS Compeleted ");
        }

        @Override
        public void onError(String utteranceId) {
        }

        @Override
        public void onStart(String utteranceId) {
        }
      });
     } else {
    Log.e("YourActivity", " TTS Failed!");
   }
}

Deprecated TextToSpeech.OnUtteranceCompletedListener

答案 1 :(得分:1)

尝试

textToSpeech=new TextToSpeech(this, new TextToSpeech.OnInitListener() {
    @Override
    public void onInit(int status) {
        if (status==TextToSpeech.SUCCESS){
            int result=textToSpeech.setLanguage(Locale.ENGLISH);

            if (result==TextToSpeech.LANG_MISSING_DATA||result==TextToSpeech.LANG_NOT_SUPPORTED){
                Log.i("TextToSpeech","Language Not Supported");
            }

            textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
                @Override
                public void onStart(String utteranceId) {
                    Log.i("TextToSpeech","On Start");
                }

                @Override
                public void onDone(String utteranceId) {
                    Log.i("TextToSpeech","On Done");
                }

                @Override
                public void onError(String utteranceId) {
                    Log.i("TextToSpeech","On Error");
                }
            });

        }else {
            Log.i("TextToSpeech","Initialization Failed");
        }
    }
});

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null,TextToSpeech.ACTION_TTS_QUEUE_PROCESSING_COMPLETED);
}

答案 2 :(得分:0)

覆盖TextToSpeech.OnUtteranceCompletedListener方法。有关完整文档here. 当TTS服务完成合成话语时将调用的监听器。

  @Override
    public void onUtteranceCompleted(String utteranceId) {
        Log.i("CALLBACK", utteranceId);

    }

希望它能帮到你!!