问题:发言失败:未绑定到tts engine
我正在实现textToSpeech
功能。我得到例外,因为说话失败:不受tts engine
约束。我正在用它实现async task
。async task
将阅读mail
。我想转换mail body to speech
.
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);
而且 - 最重要的是 - 不要在活动派生类中使用构造函数:
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);
}
您的谷歌文字转语音引擎可能会被禁用...一旦检查您的设置
如果禁用,它也会显示相同的错误