TextToSpeech API - ** - 请帮助 - ** - 调用方法

时间:2011-02-25 17:38:17

标签: android

我在API演示中使用了这个类:

public class TextToSpeechActivity extends Activity implements TextToSpeech.OnInitListener {

     static int currentHelloIndex = 0;
private static final String TAG = "TextToSpeechDemo";

public static TextToSpeech mTts;
private Button mAgainButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.text_to_speech);

    // Initialize text-to-speech. This is an asynchronous operation.
    // The OnInitListener (second argument) is called after initialization completes.
    mTts = new TextToSpeech(this,
        this  // TextToSpeech.OnInitListener
        );

    // The button is disabled in the layout.
    // It will be enabled upon initialization of the TTS engine.
    mAgainButton = (Button) findViewById(R.id.again_button);

    mAgainButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sayHello();
        }
    });
}

@Override
public void onDestroy() {
    // Don't forget to shutdown!
    if (mTts != null) {
        mTts.stop();
        mTts.shutdown();
    }

    super.onDestroy();
}

// Implements TextToSpeech.OnInitListener.
@Override
public void onInit(int status) {
    // status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
    if (status == TextToSpeech.SUCCESS) {
        // Set preferred language to US english.
        // Note that a language may not be available, and the result will indicate this.
        int result = mTts.setLanguage(Locale.US);
        // Try this someday for some interesting results.
        // int result mTts.setLanguage(Locale.FRANCE);
        if (result == TextToSpeech.LANG_MISSING_DATA ||
            result == TextToSpeech.LANG_NOT_SUPPORTED) {
           // Lanuage data is missing or the language is not supported.
            Log.e(TAG, "Language is not available.");
        } else {
            // Check the documentation for other possible result codes.
            // For example, the language may be available for the locale,
            // but not for the specified country and variant.

            // The TTS engine has been successfully initialized.
            // Allow the user to press the button for the app to speak again.
            mAgainButton.setEnabled(true);
            // Greet the user.
            sayHello();
        }
    } else {
        // Initialization failed.
        Log.e(TAG, "Could not initialize TextToSpeech.");
    }
}


private static final String[] HELLOS = {
  "What is a string of words that satisfy the grammatical rules of a sentence",
  "Salutations",
  "Greetings",
  "Howdy",
  "What's crack-a-lackin?",
  "That explains the stench!"
};


public static void sayHello() {
    // Select a random hello.

    int helloLength = HELLOS.length;
    String hello = HELLOS[currentHelloIndex];
    currentHelloIndex = (currentHelloIndex + 1) % helloLength;
    mTts.speak(hello,
        TextToSpeech.QUEUE_FLUSH,  // Drop all pending entries in the playback queue.
        null);


  }

}

然后我有另一个带有Onclick按钮的类并使用sayHello();调用TextToSpeech.sayHello();方法这给了我一个进程关闭错误,并且在logcat源中找不到null异常错误等。请有人可以帮我解决这个问题。感谢

1 个答案:

答案 0 :(得分:0)

您当前的设计存在重大缺陷。 mTts仅在onCreate的{​​{1}}方法中初始化,但您尝试在另一个类的TextToSpeechActivity方法中调用此对象上的方法。当您的其他班级调用static时,无法确定mTts不是null。此外,即使sayHello()不为空,也会将mTts传递给构造函数,这是this的实例,所以我不知道TextToSpeechActivity是否会工作为完全不同的TextToSpeech创建时。

我认为你需要重新思考你的班级设计。