输入活动时的文字转语音

时间:2014-09-23 15:10:08

标签: android text-to-speech

我想在输入后告诉每个活动的标题。我已经制作了一个代码,但是当我点击按钮时它会告诉标题,因为我不知道如何在没有按钮的情况下执行此操作。

有人可以为我修改一下,这样一旦活动开启,TTS就会启动吗?

这是我的代码

public class Mode extends Activity implements
TextToSpeech.OnInitListener {

private TextToSpeech tts;
private Button btnSpeak;


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

    tts = new TextToSpeech(this, this);
    btnSpeak = (Button) findViewById(R.id.btnSpeak);        

    btnSpeak.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            speakOut("Choose your Mode");
        }

    });
}

@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 {

        }

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

}

private void speakOut(String text) {

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

}

2 个答案:

答案 0 :(得分:1)

使用处理程序怎么样?例如:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
     speakOut("Choose your Mode");  //speak after 1000ms
  }
}, 1000);

在这个例子中(在onCreate里面),它将在1000ms后执行..

答案 1 :(得分:0)

package com.varshney.voicebasedchatapp;

import android.content.Intent;
import android.os.CountDownTimer;
import android.os.Handler;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.util.Locale;

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
    TextToSpeech tts;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tts = new TextToSpeech(this, this);
        new CountDownTimer(5000, 1500) {
            @Override
            public void onTick(long l) {

                        speakOut("Welcome to VoiceBased Chat Application");  //speak after 1000ms


            }

            @Override
            public void onFinish() {
              Intent intent= new Intent(MainActivity.this,Login.class);
              startActivity(intent);
            }
        }.start();
    }

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

    @Override
    public void onInit(int i) {
        if (i == 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 {

            }

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

    private void speakOut(String text) {

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


}