联系方式:xidianwx@126.com
Android sdk提供TTS的接口,可以使用现成的类,将文本转化为语音,但是,不支持中文。
这次开发的应用需要用到中文TTS,我想对于一个学生而言开发自己的TTS引擎实在是异想天开,我还真没有这能力。于是就在网上找找看,有没有支持中文的TTS应用,最终选定了Svox。Svox支持十几种语言,包括中文普通话和中文粤语。貌似svox是付费软件,各种语言包也是要购买的,但是。。。这是在china
import android.speech.tts.TextToSpeech;
private TextToSpeech tts = null;
tts = new TextToSpeech(this,null);
tts.speak("我能说中文", TextToSpeech.QUEUE_FLUSH, null);
sdk解释:
public TextToSpeech (Context context, TextToSpeech.OnInitListener listener)
Since: API Level 4
The constructor for the TextToSpeech class. This will also initialize the associated TextToSpeech engine if it isn't already running.
Parameters
context The context this instance is running in.
listener The TextToSpeech.OnInitListener that will be called when the TextToSpeech engine has initialized.
public int speak (String text, int queueMode, HashMap<String, String> params)
Since: API Level 4
Speaks the string using the specified queuing strategy and speech parameters.
Parameters
text The string of text to be spoken.
queueMode The queuing strategy to use. QUEUE_ADD or QUEUE_FLUSH.
params The list of parameters to be used. Can be null if no parameters are given. They are specified using a (key, value) pair, where the key can be KEY_PARAM_STREAM or KEY_PARAM_UTTERANCE_ID.
Returns
• Code indicating success or failure. See ERROR and SUCCESS.
运行的时候要设置svox为系统默认的TTS引擎,设置步骤如下:
设置 -> 语音输入与输出 -> 文字转语音设置 -> 总是使用我的设置 打钩
-> 默认引擎 选择Svox Classic TTS
->引擎 选中Svox Classic TTS
这样做的坏处是安装应用的时候还要额外安装svox和语言包。要是有开源的中文TTS引擎就好了,直接引入jar包。