简体   繁体   English

TextToSpeech在Android 2.2和2.3 Phone中不起作用

[英]TextToSpeech not working in Android 2.2 and 2.3 Phone

What I am doing is I have an EditText and a button. 我正在做的是有一个EditText和一个按钮。 When I write something and press the button, the app speaks those words. 当我写东西并按下按钮时,应用会说出这些单词。 This is working fine in Emulator but when I run it on phone, it does not speak. 这在Emulator中工作正常,但是当我在电话上运行它时,它不会讲话。 I don't know what is the problem. 我不知道是什么问题。 Is it something with the code or do I need to change something in the phone. 是代码中的某些内容还是我需要在手机中进行更改? Below is the code that I have used. 下面是我使用的代码。

Button btn1;
EditText txt1;
static final String[] texts={""};
TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn1=(Button)findViewById(R.id.button1);
    txt1=(EditText)findViewById(R.id.editText1);
    btn1.setOnClickListener(this);
    tts=new TextToSpeech(MainActivity.this,new TextToSpeech.OnInitListener() {

        @Override
        public void onInit(int status) {
            // TODO Auto-generated method stub
            if(status!=TextToSpeech.ERROR){
                tts.setLanguage(Locale.getDefault());
            }
        }
    });
}

    @Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
        //Random r=new Random();
        //String random=texts[r.nextInt(3)];
        String txt=txt1.getText().toString();
        if(txt!="")
        tts.speak(txt, TextToSpeech.QUEUE_FLUSH, null);     
}

I checked out the other similar questions but none has a satisfying answer. 我检查了其他类似的问题,但没有一个令人满意的答案。 This is the best place to get technical help. 这是获得技术帮助的最佳位置。 Plz suggest a solution to this problem. Plz建议解决此问题。

I got the solution. 我找到了解决方案。 No TTS language was installed on my phone so i did it manually : Settings --> Language and input --> Text to speech output --> Install voice data. 我的手机上没有安装TTS语言,所以我手动进行了:设置->语言和输入->文本到语音输出->安装语音数据。

please try 请试试

if(!txt.equals("")) tts.speak(txt, TextToSpeech.QUEUE_FLUSH, null); if(!txt.equals(“”))tts.speak(txt,TextToSpeech.QUEUE_FLUSH,null);

Also release the tts object after the usage using shutdown() method. 使用后,还要使用shutdown()方法释放tts对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM