1,入门:让TEXT朗读起来
官方TTS朗读支持语言种类 |
|
English |
英语 |
French |
法语 |
German |
德语 |
Italian |
意大利语 |
Spanish |
西班牙语 |
不支持中文… |
|
//1,先写一个用来检查tts是否安装的方法
private final static int CHECK_CODE = 1;
public void checkTts(){
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, CHECK_CODE);
}
//创建一个TTS
private TextToSpeech mTts;
//这个自己先去了解下onActivityResult是怎么回事,这里不作详解
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CHECK_CODE){
if(resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){
//成功创建一个TTS
mTts = new TextToSpeech(this, this);
}else{
//否则安装一个
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
//2,让TTS朗读起来
//onCreate方法中
//检查TTS是否正常
checkTts();
//创建用组件
private speakValue = (EditText) findViewById(R.id.speakValue);
private speakButton = (Button) findViewById(R.id.sayHello);
speakButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String text = speakValue.getText().toString();
Log.d("result","text-->" +text);
sayTts(text);
}
});
//TTs朗读用
private void sayTts(String text){
//最简单的例子
mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
//补充,使用TTS朗读,一定要实现OnInitListener 接口
//TTs对象创建后初始化
@Override
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS){
//设置语言区域
int result = mTts.setLanguage(Locale.US);
//如果是不支持语言
if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){
Log.e("error","不支持");
}
}
}
就这么点代码就可以正常运行了…
2,进阶使用:改变我们朗读播放的类型,回调函数的使用与自定义文字发音
1,更改播放的流类型(暂且这样说,希望,有人解释一下)
//更改播放使用的流类型
//在sayTts作以下修改
//TTs朗读用
private void sayTts(String text)
{
HashMap<String, String> myAlarm = new HashMap();
//把播放类型,通过闹钟流实现
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
String.valueOf(AudioManager.STREAM_ALARM));
//你播放的tts在音频播放的闹钟类型流中
mTts.speak(text, TextToSpeech.QUEUE_FLUSH, myAlarm);
//英语原文是这样,我了解的就是以上我注释的内容
//我感觉不是很了解,希望有人帮帮
//On Android, each audio stream that is played is associated with one //stream type, as defined in android.media.AudioManager. For a talking //alarm clock, we would like our text to be played on the //AudioManager.STREAM_ALARM stream type so that it respects the alarm //settings the user has chosen on the device.
}
2,回调函数的使用
//在sayTts()中实现..
//需要实现OnUtteranceCompletedListener这个接口
//TTs朗读用
private Toast toast;
prvate Context context = getApplicationContext();
private void sayTts(String text)
{
HashMap<String, String> myAlarm = new HashMap();
mTts.setOnUtteranceCompletedListener(this);
myAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "me too");
toast = Toast.makeText(context, "me too", Toast.LENGTH_LONG);
//最简单的例子
mTts.speak(text, TextToSpeech.QUEUE_FLUSH, myAlarm);
}
//回调函数
@Override
public void onUtteranceCompleted(String utteranceId) {
speakPlayBack(utteranceId);
}
private void speakPlayBack(String str) {
Log.d("result","playBack-->" + str);
toast.show();
}
3,录制TTS朗读保存到SD卡中
//把TTS朗读结果保存
private void sayTofile(String text)
{
HashMap<String, String> ttsRender = new HashMap<String, String>();
String destFileName = "/sdcard/tts/"+text+".wav";
ttsRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, text);
mTts.synthesizeToFile(text, ttsRender, destFileName);
}
//在onclick方法中运行sayTofile()即可
你就可以看到你刚才朗读的Text被保存了
4,自定义Text发音
//自定义文字播放
private void sayTrue(String text)
{
String destFileName = "/sdcard/tts/"+text+".wav";
mTts.addSpeech(text, destFileName);
mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
//在onclick方法中运行
sayTrue();
亲测,MP3也可以播放…
5,销毁
@Override
protected void onDestroy() {
super.onDestroy();
if(mTts != null){
mTts.stop();
mTts.shutdown();
}
}