安卓语音播放的Java类
安卓语音播放的Java类
·
系统默认只支持英文,支持中文的要安装语音包
百度语音包
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.widget.Toast;
import java.util.Locale;
/**
* Created by zhenqiang on 2016/12/9.
*/
public class SpeechUtils {
private Context context;
private static final String TAG = "SpeechUtils";
private static SpeechUtils singleton;
private TextToSpeech textToSpeech; // TTS对象
public static SpeechUtils getInstance(Context context) {
if (singleton == null) {
synchronized (SpeechUtils.class) {
if (singleton == null) {
singleton = new SpeechUtils(context);
}
}
}
return singleton;
}
public SpeechUtils(Context context) {
this.context = context;
textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
if (i == TextToSpeech.SUCCESS) {
textToSpeech.setLanguage(Locale.US);
textToSpeech.setLanguage(Locale.CHINESE);
textToSpeech.setPitch(1.0f);// 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
textToSpeech.setSpeechRate(1.0f);
}
}
});
}
public void speakText(String text) {
if (textToSpeech != null) {
textToSpeech.speak(text,
TextToSpeech.QUEUE_FLUSH, null);
}
}
}
更多推荐
所有评论(0)