Android TextToSpeech addSpeech() is not working

2019-08-14 07:21发布

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条回答
SAY GOODBYE
2楼-- · 2019-08-14 07:41

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);
    //...
}
查看更多
登录 后发表回答