0%

Android TTS 中文 文字转语音 使用TextToSpeech Svox

Android默认没有安装TTS数据包,无法文字转语音,而在设置里推荐的语音包是Pico TTS,并不支持中文,如果需要读中文,需要下载另外的第三方语音包,如:eSpeak,Svox,个人建议Svox,eSpeak非常生硬,而且很多汉字都读不出,不支持中英文混读(英文当拼音读)。下载链接自己Google下吧,很多软件市场都有下载。下载安装后打开设置-语音输入和输出-文字转语音设置,勾选Svox Classic TTS,语言选择中文或广东话(Svox安装后还要下载安装相应的语言包的,类似插件)。接下来代码就简单了,关键的就两三行:

package com.pocketdigi;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TTSActivity extends Activity {
    /** Called when the activity is first created. */
    TextToSpeech tts;
    Button btn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn=(Button)findViewById(R.id.btn);
        tts = new TextToSpeech(this, null);
        //实例化
        btn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                tts.speak("测试一下", TextToSpeech.QUEUE_FLUSH, null);
                //语音输出
            }});
        
    }
}

用Svox,效果还不错。 2011年7月21日加注: 貌似使用Svox后,isLanguageAvailable就不能检测语言是否支持了,而Pico TTS是可以的