Java 文本到语音自动播放

Java 文本到语音自动播放,java,android,text-to-speech,Java,Android,Text To Speech,我的android应用程序中有一个文本到语音的功能,可用于onClick事件,活动开始时出现问题,文本到语音在未单击按钮的情况下开始,是否有一行代码可以输入以阻止这种情况发生,谢谢 package com.androidhive.texttospeech; import java.util.Locale; import android.app.Activity; import android.os.Bundle; import android.speech.tts.TextToSpeech;

我的android应用程序中有一个文本到语音的功能,可用于onClick事件,活动开始时出现问题,文本到语音在未单击按钮的情况下开始,是否有一行代码可以输入以阻止这种情况发生,谢谢

package com.androidhive.texttospeech;

import java.util.Locale;

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.TextView;

public class AndroidTextToSpeechActivity extends Activity implements
    TextToSpeech.OnInitListener {
/** Called when the activity is first created. */

    private TextToSpeech tts;
    private Button button1;
    private TextView txtText;

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

    tts = new TextToSpeech(this, this);

    button1 = (Button) findViewById(R.id.button1);

    txtText = (TextView) findViewById(R.id.txtText);

    // button on click event
    button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            speakOut();
        }

    });
 }

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

@Override
public void onInit(int status) {
    // TODO Auto-generated method stub

    if (status == TextToSpeech.SUCCESS) {

        int result = tts.setLanguage(Locale.US);

        // tts.setPitch(5); // set pitch level

        // tts.setSpeechRate(2); // set speech speed rate

        if (result == TextToSpeech.LANG_MISSING_DATA
                || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "Language is not supported");
        } else {
            button1.setEnabled(true);
            speakOut();
        }

    } else {
        Log.e("TTS", "Initilization Failed");
    }

    }

 private void speakOut() {

    String text = txtText.getText().toString();

    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
   }
}

我在onInit()中看到了这些代码,在成功实例化和初始化TextToSpeech后将调用这些代码。看到这里的扬声器了吗?这就是你的问题所在

if (result == TextToSpeech.LANG_MISSING_DATA
            || result == TextToSpeech.LANG_NOT_SUPPORTED) {
        Log.e("TTS", "Language is not supported");
} else {
    button1.setEnabled(true);
    speakOut();
}

发生这种情况的原因是,在onInit()方法中调用speakOut,remove speakOut():


我才意识到,对不起,没问题。发生了:)你有没有机会回答我在标签上的另一个问题?:)检查布局文件,并检查min sdk版本。您还应该高亮显示第126行
 if (result == TextToSpeech.LANG_MISSING_DATA
                || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "Language is not supported");
        } else {
            button1.setEnabled(true);
          //  speakOut();
        }