Android TextToSpeech addSpeech() is not working

心已入冬 提交于 2019-12-24 09:36:57

问题


I have an HD Desire phone with android 2.3.

The TTS is working fine and it speaks every text I give. But when I use this either of the lines below to set my own voice for some texts, it simply ignores it and synthesizes the text, just like the line is not written!

tts.addSpeech("salam", "/sdcard/salam.wav");
tts.addSpeech("shalam", "com.company.appname", R.raw.shalam);
...
tts.speak("salam", TextToSpeech.QUEUE_FLUSH, null);  //<--This isn't playing my voice file.
tts.speak("shalam", TextToSpeech.QUEUE_FLUSH, null);  //<--Neither is this

I am sure of the existence of both files. Why is that? Is there any restriction on the sound files? For example on their Frequency, or being mono or stereo?

I already checked the docs and saw nothing related.


回答1:


OK, I found my problem, very silly situation which wasted several hours of mine!! I hope it will help if someone makes my mistake.

We should postpone this mapping of texts to the point TTS is successfully initialized, for example in onInit function:

@Override
public void onInit(int status) {
    if(status == TextToSpeech.SUCCESS)
    {
        tts.setLanguage(Locale.US);
        mapVoices();
    }
    else
        ...
}

private void mapVoices()
{
    tts.addSpeech("salam", "/sdcard/salam.wav");
    tts.addSpeech("shalam", "com.company.appname", R.raw.shalam);
    //...
}


来源:https://stackoverflow.com/questions/17123410/android-texttospeech-addspeech-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!