Text to Speech 说完如何敬酒 Android
How can I Toast after Text to Speech finish speaking Android
Text to Speech 讲完如何敬酒。其实我想做的不仅仅是日志。
这是我的代码。
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说完之后调用speech to text。如何在这个方法中做一些事情。
03-14 14:35:16.652 5473-5489/com.example.thummawit.testttscallback I/CALLBACK: Hello
03-14 14:35:16.667 5473-5489/com.example.thummawit.testttscallback W/Binder: Caught a RuntimeException from the binder stub implementation.
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.(Handler.java:200)
at android.os.Handler.(Handler.java:114)
at android.widget.Toast$TN.(Toast.java:459)
at android.widget.Toast.(Toast.java:120)
at android.widget.Toast.makeText(Toast.java:289)
at com.example.thummawit.testttscallback.MainActivity.onUtteranceCompleted(MainActivity.java:59)
at android.speech.tts.UtteranceProgressListener.onDone(UtteranceProgressListener.java:73)
at android.speech.tts.TextToSpeech$Connection.onSuccess(TextToSpeech.java:2158)
at android.speech.tts.ITextToSpeechCallback$Stub.onTransact(ITextToSpeechCallback.java:63)
at android.os.Binder.execTransact(Binder.java:446)
您尝试在非 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
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);
}
Text to Speech 讲完如何敬酒。其实我想做的不仅仅是日志。 这是我的代码。
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说完之后调用speech to text。如何在这个方法中做一些事情。
03-14 14:35:16.652 5473-5489/com.example.thummawit.testttscallback I/CALLBACK: Hello 03-14 14:35:16.667 5473-5489/com.example.thummawit.testttscallback W/Binder: Caught a RuntimeException from the binder stub implementation. java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at android.os.Handler.(Handler.java:200) at android.os.Handler.(Handler.java:114) at android.widget.Toast$TN.(Toast.java:459) at android.widget.Toast.(Toast.java:120) at android.widget.Toast.makeText(Toast.java:289) at com.example.thummawit.testttscallback.MainActivity.onUtteranceCompleted(MainActivity.java:59) at android.speech.tts.UtteranceProgressListener.onDone(UtteranceProgressListener.java:73) at android.speech.tts.TextToSpeech$Connection.onSuccess(TextToSpeech.java:2158) at android.speech.tts.ITextToSpeechCallback$Stub.onTransact(ITextToSpeechCallback.java:63) at android.os.Binder.execTransact(Binder.java:446)
您尝试在非 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
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);
}