当其从所谓的OnStart API文字转语音不工作 [英] TextToSpeech API doesn't work when its called from OnStart

查看:162
本文介绍了当其从所谓的OnStart API文字转语音不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的文字转语音API在我的code,当我尝试从的OnStart()调用.speak()函数,它不工作,但它的工作原理,当我把它从一个按钮onClickListener() 。知道为什么吗?谢谢你。

 公共类TtsDemoActivity延伸活动{    私人文字转语音MTTS;
    私人OnClickListener buttonListener =新View.OnClickListener(){        @覆盖
        公共无效的onClick(查看为arg0){
            播放声音();
        }
    };    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.ttsdemo);        //初始化文本到语音
        MTTS =新的文字转语音(这一点,新TextToSpeech.OnInitListener(){            @覆盖
            公共无效的OnInit(INT为arg0){
                // TODO自动生成方法存根            }
        });        按钮则myButton =(按钮)findViewById(R.id.buttontts1);
        myButton.setOnClickListener(buttonListener);
    }    @覆盖
    保护无效调用onStart(){
        // TODO自动生成方法存根
        super.onStart();
        播放声音();
    }    保护无效PlaySound()
    {
        串词=Hello World的;        mTts.speak(文字,TextToSpeech.QUEUE_FLUSH,NULL);    }


解决方案

您必须等到TTS子系统的信号,它已准备就绪:如果还没有准备好当在onStart 被调用,它就会失败。如果你想为它准备呼吁尽快讲 PlaySound 从里面的 OnInitListener

  @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.ttsdemo);        //初始化文本到语音
        MTTS =新的文字转语音(这一点,新TextToSpeech.OnInitListener(){            @覆盖
            公共无效的OnInit(INT为arg0){
如果(arg0中== TextToSpeech.SUCCESS)PlaySound();            }
        });        按钮则myButton =(按钮)findViewById(R.id.buttontts1);
        myButton.setOnClickListener(buttonListener);
    }

I'm using the TextToSpeech API in my code and it doesn't work when I try to call .speak() function from OnStart(), however it works when I call it from a button onClickListener(). Any idea why? Thank you.

public class TtsDemoActivity extends Activity {

    private TextToSpeech mTts;
    private OnClickListener buttonListener = new View.OnClickListener() {

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ttsdemo);

        // Initialize Text To speech
        mTts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {

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

            }
        });

        Button myButton = (Button)findViewById(R.id.buttontts1);
        myButton.setOnClickListener(buttonListener);
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        PlaySound();
    }

    protected void PlaySound()
    {
        String word = "Hello world";

        mTts.speak(word, TextToSpeech.QUEUE_FLUSH, null);

    }

解决方案

You must wait until the TTS subsystem signals that it is ready: if it isn't ready when onStart is called, it will fail. If you are trying to speak as soon as it is ready call PlaySound from inside the OnInitListener:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ttsdemo);

        // Initialize Text To speech
        mTts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int arg0) {
if(arg0 == TextToSpeech.SUCCESS) PlaySound();

            }
        });

        Button myButton = (Button)findViewById(R.id.buttontts1);
        myButton.setOnClickListener(buttonListener);
    }

这篇关于当其从所谓的OnStart API文字转语音不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆