要说Android的文字不起作用

时间:2014-11-17 17:52:46

标签: java android text-to-speech

我试图在我的应用上实现Text to Speech。我已按照以下指南操作,但我的应用在启动画面后仍然崩溃。

link:http://www.androidhive.info/2012/01/android-text-to-speech-tutorial/

主要活动类我有这个

   String alertMessage = AppResources.ALERT_MSG;


SpeakDemo speak = new SpeakDemo(null, alertMessage);
speak.speakOut();

如果我删除speak.speakOut();该应用程序正常运行。

App Resources是另一个带有以下消息框的类

    public static String ALERT_MSG = "Welcome!";

SpeakDemo类是:

import java.util.Locale;

import android.content.Context; 
 import android.speech.tts.TextToSpeech;
  import android.util.Log;
   public class SpeakDemo  implements TextToSpeech.OnInitListener{

private TextToSpeech tts;
private Context context;
private String message;

public SpeakDemo(Context context, String message){
this.context = context; 
this.message = message;
}


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


    if (status == TextToSpeech.SUCCESS) {

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

        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!");
    }

}


public void speakOut(){

    tts.speak(message, TextToSpeech.QUEUE_FLUSH, null);



}

}

任何想法?

由于

3 个答案:

答案 0 :(得分:0)

您需要实例化TextToSpeech。

更改

public SpeakDemo(Context context, String message){
    this.context = context; 
    this.message = message;
    tts = new TextToSpeech(context, this);
}

答案 1 :(得分:0)

//FOR TEXT TO SPEECH
@Override
public void onInit(int status) {
    // TODO Auto-generated method stub
    String msg=text.getText().toString();
    if(status==TextToSpeech.SUCCESS)
    {
        tts.setLanguage(Locale.US);
        tts.speak(msg,TextToSpeech.QUEUE_FLUSH,null);
    }
    else 
        Log.e("TTS","INITILIZATION FAILED");
}
public void onDestroy()
{
    if (tts!=null)
    {
        tts.stop();
        tts.shutdown();
    }
    super.onDestroy();
}

答案 2 :(得分:0)

Xml:
  <Button

        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Speak"
     android:onClick="TTS"/>
Mainactivity:
TextToSpeech textToSpeech; 
textToSpeech=new TextToSpeech(TTSpeech.this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if(status==TextToSpeech.SUCCESS)
                {
                    result=textToSpeech.setLanguage(Locale.ENGLISH);

                }
                else
                {
                    Toast.makeText(TTSpeech.this, "speech not work", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
 public void TTS(View view) {
        switch (view.getId())
        {
            case R.id.button:
                if(result== TextToSpeech.LANG_NOT_SUPPORTED || result==TextToSpeech.LANG_MISSING_DATA)
                {
                    Toast.makeText(TTSpeech.this, "speech not  nt  work", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    text=ed.getText().toString();
                    textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null);
                }
                break;
}
if you get problem you can comment me.