Android调用自带TTS文本转语音引擎实现离线语音合成

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

开工,第一步,初始化:

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支持多个人物的语音切换,可以自行下载这个引擎。

完。

 

 

你可能感兴趣的:(Android)