提问者:小点点

如何在文本转语音说完Android后吐司


文本转语音后如何吐司?实际上我想做的不仅仅是日志。这是我的代码。

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
    }

实际上我想在Text to Speech完成说话后调用语音到文本。如何在这个方法中做某事。

03-14 14:35:16.652 5473-5489/com.example.thumwit.testttscallback I/CALLBACK:Hello 03-14 14:35:16.667 5473-5489/com.example.thumwit.testttscallback W/Binder:从binder存根实现捕获RuntimeException.java.lang.RuntimeException:无法在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.thumwit.testttscallback.MainActivity.onUtteranceCompl(MainActivity.java:59)在android.phone.tts.UtteranceProgressListener1 dol.onDone(UtteranceProgressListener.java:73)在


共2个答案

匿名用户

您尝试在不是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();
        }
    });
}

这样您的代码就会被分派到允许您显示Toasts的主线程

匿名用户

onUtteranceCompled已弃用。使用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);
}