首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >文本到语音自动播放

文本到语音自动播放
EN

Stack Overflow用户
提问于 2014-12-04 14:58:56
回答 2查看 1.7K关注 0票数 0

我在我的安卓应用程序中有一个文本到语音功能,它适用于一个onClick事件,当活动开始时,文本到语音开始时没有点击按钮,我是否可以插入一行代码来阻止这种情况发生,谢谢。

代码语言:javascript
复制
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);
   }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-04 15:13:52

之所以发生这种情况,是因为您在onInit()方法中调用了onInit()方法,删除了speakOut():

代码语言:javascript
复制
 if (result == TextToSpeech.LANG_MISSING_DATA
                || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "Language is not supported");
        } else {
            button1.setEnabled(true);
          //  speakOut();
        }
票数 0
EN

Stack Overflow用户

发布于 2014-12-04 15:13:43

我在onInit()中看到了这些代码,在成功实例化和初始化TextToSpeech之后将调用这些代码。看到这里的speakOut()了吗?这就是导致你的问题的原因。

代码语言:javascript
复制
if (result == TextToSpeech.LANG_MISSING_DATA
            || result == TextToSpeech.LANG_NOT_SUPPORTED) {
        Log.e("TTS", "Language is not supported");
} else {
    button1.setEnabled(true);
    speakOut();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27297140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档