java - 发言失败:not bound to tts engine

标签 java android eclipse email text-to-speech

问题:发言失败:未绑定(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/

相关文章:

java - 内部类扩展 Asynctask,外部类扩展 Activity,然后从另一个 Activity 扩展外部类。这是一个不好的做法吗?

java - 强制子类将某个字段的值设为 "implement"

java - 将 Java SE 升级到 Java EE 以及 Eclipse

java - 在枚举的构造函数中访问其他枚举

java - System.out.Println问题

java - 循环同步死锁

android - Android将Eclipse迁移到Studio

java - 使用 Java 检测 CPU 速度/内存/互联网速度?

java - 使用 Eclipse JavaSE 1.6 安装 JavaMail

java - Eclipse luna 重复方法名称