我怎样才能在文字转语音结束后完成Android演讲

时间:2016-03-14 07:48:44

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

如何在文本到语音结束后吐司说话。实际上我想做的不仅仅是Log。 这是我的代码。

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener, TextToSpeech.OnUtteranceCompletedListener {

    private TextToSpeech mTts;
    Button btnSpeak;
    EditText editTextTTS;

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

        mTts = new TextToSpeech(this,this);
        editTextTTS =(EditText)findViewById(R.id.editText);
        btnSpeak = (Button)findViewById(R.id.btnSpeakTest);
        btnSpeak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                speak(editTextTTS.getText().toString());
            }
        });




    }
    private void speak(String word){
        if(word != null) {
            HashMap<String, String> myHashAlarm = new HashMap<String, String>();
            myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM));
            myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "Hello");
            mTts.speak(word, TextToSpeech.QUEUE_FLUSH, myHashAlarm);
        }
    }

    @Override
    public void onInit(int status) {
        if(status == TextToSpeech.SUCCESS) {
            mTts.setOnUtteranceCompletedListener(this);
        }
    }

    @Override
    public void onUtteranceCompleted(String utteranceId) {
        Log.i("CALLBACK", utteranceId); //utteranceId == "SOME MESSAGE"
        Toast.makeText(getApplicationContext(),"Call Back",Toast.LENGTH_LONG).show();// I Cannot Toast here. Or do something more than Log
    }

实际上,我希望在文本到语音完成发言之后将语音称为文本。如何在这种方法中做点什么。

  

03-14 14:35:16.652 5473-5489 / com.example.thummawit.testttscallback I / CALLBACK:您好   03-14 14:35:16.667 5473-5489 / com.example.thummawit.testttscallback W / Binder:从绑定存根实现中捕获了RuntimeException。                                                                                java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序                                                                                    在android.os.Handler。(Handler.java:200)                                                                                    在android.os.Handler。(Handler.java:114)                                                                                    在android.widget.Toast $ TN。(Toast.java:459)                                                                                    在android.widget.Toast。(Toast.java:120)                                                                                    在android.widget.Toast.makeText(Toast.java:289)                                                                                    在com.example.thummawit.testttscallback.MainActivity.onUtteranceCompleted(MainActivity.java:59)                                                                                    在android.speech.tts.UtteranceProgressListener $ 1.onDone(UtteranceProgressListener.java:73)                                                                                    在android.speech.tts.TextToSpeech $ Connection $ 1.onSuccess(TextToSpeech.java:2158)                                                                                    在android.speech.tts.ITextToSpeechCallback $ Stub.onTransact(ITextToSpeechCallback.java:63)                                                                                    在android.os.Binder.execTransact(Binder.java:446)

2 个答案:

答案 0 :(得分:7)

您尝试在不是UI(主)线程的线程中显示Toast。 你应该改变这个

@Override
public void onUtteranceCompleted(String utteranceId) {
    Log.i("CALLBACK", utteranceId); //utteranceId == "SOME MESSAGE"
    Toast.makeText(getApplicationContext(),"Call     Back",Toast.LENGTH_LONG).show();// I Cannot Toast here. Or do something more than Log
}

进入这个

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

    runOnUiThread(new Runnable() {

        public void run() {
            Toast.makeText(getApplicationContext(),"Call Back",Toast.LENGTH_LONG).show();
        }
    });
}

这样您的代码就会被分派到主线程,在那里您可以显示Toast s

答案 1 :(得分:1)

onUtteranceCompleted已过时。使用OnUtteranceProgressListener

代码段

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);
}