只要手机带有语音助手的话一般就会存在中文语音引擎,如果没有的话需要额外下载中文的语音引擎,不然不支持中文。

开工,第一步,初始化:

private TextToSpeech tts;tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {    @Override    public void onInit(int status) {        if (status == TextToSpeech.SUCCESS) {            int result = tts.setLanguage(Locale.CHINA);            if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE && result != TextToSpeech.LANG_AVAILABLE) {                Toast.makeText(MainActivity.this, "TTS不支持中文", Toast.LENGTH_SHORT).show();            }        }    }});

第二步,控制:

//设置语速tts.setSpeechRate(1.0f);//设置语调tts.setPitch(1.5f);//开始朗读tts.speak("朗读的文字内容", TextToSpeech.QUEUE_FLUSH, null,getString(R.string.app_name));

回调监听:

tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {            @Override            public void onStart(String utteranceId) {                //utteranceId是speak方法中最后一个参数:唯一标识码            }            @Override            public void onDone(String utteranceId) {            }            @Override            public void onError(String utteranceId) {                //这个onError方法已过时,用下面这个方法代替            }            @Override            public void onError(String utteranceId,int errorCode) {                //这个方法代替上面那个过时方法            }        });

OK,代码部分完成,收工。接下来是参数解释:

setSpeechRate(float speechRate)

  • speechRate:速率:默认是1.0f,可选0.5f~2.0f

 

setPitch(float pitch)

  • pitch:音调:默认是1.0。貌似这个参数的设置不怎么起作用。

 

speak(final CharSequence text, final int queueMode,final Bundle params,final String utteranceId)

  • text:朗读的内容(长度4000以内)
  • queueMode:队列模式:
    • 可选参数:
      • TextToSpeech.QUEUE_FLUSH:打断已有朗读,立即朗读这段。
      • TextToSpeech.UEUE_ADD:读完正在读的再读这段。
  • params:请求的参数。可为空。
    • 可选参数名称:
      • TextToSpeech.KEY_PARAM_STREAM
      • TextToSpeech.KEY_PARAM_VOLUME
      • TextToSpeech.KEY_PARAM_PAN
  • utteranceId:这个请求唯一的标识。

 

setLanguage(final Locale loc)

  • loc:语种
    • 可选参数:(以下是源码,可选参数为以下的字段名)
/** Useful constant for country.     */    static public final Locale FRANCE = createConstant("fr", "FR");    /** Useful constant for country.     */    static public final Locale GERMANY = createConstant("de", "DE");    /** Useful constant for country.     */    static public final Locale ITALY = createConstant("it", "IT");    /** Useful constant for country.     */    static public final Locale JAPAN = createConstant("ja", "JP");    /** Useful constant for country.     */    static public final Locale KOREA = createConstant("ko", "KR");    /** Useful constant for country.     */    static public final Locale CHINA = SIMPLIFIED_CHINESE;    /** Useful constant for country.     */    static public final Locale PRC = SIMPLIFIED_CHINESE;    /** Useful constant for country.     */    static public final Locale TAIWAN = TRADITIONAL_CHINESE;    /** Useful constant for country.     */    static public final Locale UK = createConstant("en", "GB");    /** Useful constant for country.     */    static public final Locale US = createConstant("en", "US");    /** Useful constant for country.     */    static public final Locale CANADA = createConstant("en", "CA");    /** Useful constant for country.     */    static public final Locale CANADA_FRENCH = createConstant("fr", "CA");

 

如何切换发音人:

设置 —— 辅助功能 —— 无障碍 ——TTS文本转语音 —— 切换引擎(如果有多个引擎的话)

引擎右边设置可点击进入选择发音人(如果有的话)

科大讯飞语音引擎3.0支持多个人物的语音切换,可以自行下载这个引擎。

完。

 

 

更多相关文章

  1. 优化小技巧
  2. 【073】Android(安卓)数据存储(SQLite)
  3. android游戏引擎andengine学习系列二:简单的例子
  4. Android几种网络访问方式的比较
  5. 转Android(安卓)安全攻防(三): SEAndroid(安卓)Zygote执行用户态许
  6. Android(安卓)RotateAnimation动画不能旋转的问题
  7. android 调用系统命令实现关机
  8. Recovery模式的命令行参数
  9. android在学习——Menu背景图片,背景色的设置

随机推荐

  1. android背景图片更换――经典例子
  2. android TextView 跟随 seekBar 一起滑动
  3. EventBus的简单用法及介绍
  4. Android(安卓)Application 和Webview 之
  5. windowIsTranslucent和windowBackground
  6. eclipse开发环境下,如何根据已有代码创建
  7. Android(安卓)8.0 Adaptive Icon特性 for
  8. android TextView设置中文字体加粗
  9. Android(安卓)实现绚丽多彩的TextView
  10. RK3288 Android(安卓)5.1 固件 编译