问题:发言失败:未绑定(bind)到 tts 引擎
我正在实现textToSpeech
功能。我收到异常,因为发言失败:未绑定(bind)到 tts 引擎
。我正在用它实现异步任务
。 异步任务
将读取邮件
。我想将邮件正文转换为语音
。
package com.example.trynot;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.util.Locale;
import com.example.trynot.MainActivity.ReadMailSample;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Notify extends Activity implements TextToSpeech.OnInitListener {
/** Called when the activity is first created. */
public TextToSpeech tts = new TextToSpeech(MainActivity.c, Notify.this);
public Notify()
{
System.out.println("Inside Constructor");
speakOut();
}
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
@Override
public void onInit(int status) {
System.out.println("inside INIT");
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
tts.speak(MainActivity.ReadMailSample.command, TextToSpeech.QUEUE_FLUSH, null);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut() {
System.out.println("inside SPeak out");
tts.speak(MainActivity.ReadMailSample.command, TextToSpeech.QUEUE_FLUSH, null);
}
}
最佳答案
您应该将 tts 引擎实例的实例移至 onCreate,这一行:
public TextToSpeech tts = new TextToSpeech(MainActivity.c, Notify.this);
更改为:
public TextToSpeech tts;
并在您的onCreate
中添加:
tts = new TextToSpeech(MainActivity.c, Notify.this);
并且 - 最重要的是 - 不要在 Activity 派生类中使用构造函数:
public Notify()
{
System.out.println("Inside Constructor");
speakOut();
}
应该是你的onCreate:
@Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//speakOut(); // here tts is not yet initialized, call it in onInit on success
//tts = new TextToSpeech(MainActivity.c, Notify.this); // whats MainActivity.c?
tts = new TextToSpeech(this, this);
}
关于java - 发言失败:not bound to tts engine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35269779/