Android TextToSpeech addSpeech() 不起作用 [英] Android TextToSpeech addSpeech() is not working

查看:51
本文介绍了Android TextToSpeech addSpeech() 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一部搭载 Android 2.3 的 HD Desire 手机.

I have an HD Desire phone with android 2.3.

TTS 工作正常,它会说出我提供的每一条短信.但是当我使用下面的任何一行来为某些文本设置自己的声音时,它只是忽略它并合成文本,就像该行没有写!

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?

我已经检查了 docs 并没有看到任何相关内容.

I already checked the docs and saw nothing related.

推荐答案

好的,我发现了我的问题,非常愚蠢的情况,浪费了我几个小时的时间!!如果有人犯了我的错误,我希望它会有所帮助.

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

我们应该将这种文本映射推迟到 TTS 成功初始化的时候,例如在 onInit 函数中:

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);
    //...
}

这篇关于Android TextToSpeech addSpeech() 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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