# tts

  1. 注意播放中文 需要下载 科大讯飞语音引擎3.0 或 小爱语音引擎

实例化自带语音对象

//实例化自带语音对象
   textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
       @Override
       public void onInit(int status) {
           if (status == textToSpeech.SUCCESS) {
               //判断是否支持下面两种语言
               textToSpeech.setPitch(1.0f);//方法用来控制音调
               int result1 = textToSpeech.setLanguage(Locale.US);
               int result2 = textToSpeech.setLanguage(Locale.SIMPLIFIED_CHINESE);
               boolean a = (result1 == TextToSpeech.LANG_MISSING_DATA || result1 == TextToSpeech.LANG_NOT_SUPPORTED);
               boolean b = (result2 == TextToSpeech.LANG_MISSING_DATA || result2 == TextToSpeech.LANG_NOT_SUPPORTED);
               Log.i("zhh_tts", "US支持否?--》" + a +"\nzh-CN支持否》--》" + b);
           } else {
               Log.i("zhh_tts", "数据丢失或不支持");
           }
       }
   });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

播放语音

/**
     * queueMode 队列方式:
     * QUEUE_ADD:播放完之前的语音任务后才播报本次内容
     * QUEUE_FLUSH:丢弃之前的播报任务,立即播报本次内容
     * params 设置TTS参数,可以是null。
     * KEY_PARAM_STREAM:音频通道,可以是:STREAM_MUSIC、STREAM_NOTIFICATION、STREAM_RING等
     * KEY_PARAM_VOLUME:音量大小,0-1f
     * utteranceId:当前朗读文本的id
     * textToSpeech.speak(content, TextToSpeech.QUEUE_FLUSH, null,i+"");
     *
     * // 不管是否正在朗读TTS都被打断
     * textToSpeech.stop();
     *
     * // 关闭,释放资源
     * textToSpeech.shutdown();
     *
     * // 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
     * textToSpeech.setPitch(0.5f);
     *
     * // 设定语速,默认1.0正常语速
     * textToSpeech.setSpeechRate(1.5f);
     */
    @JavascriptInterface
    public void TTSpeech(String data) {
        textToSpeech.setSpeechRate(0.05f);
        textToSpeech.speak(data, TextToSpeech.QUEUE_ADD, null);
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27