android 7.0 tts文字转语音
【代码】android 7.0 tts文字转语音。
·
支持中文的SDK 语音引擎下载,若是报错,就换其他版本的语音引擎。
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import java.util.Locale;
public class SystemTTS {
private static final String TAG = "SystemTTS";
private static SystemTTS instance;
private TextToSpeech textToSpeech;
private boolean isSupport = true;
private SystemTTS(Context context) {
textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.CHINA);
textToSpeech.setPitch(1.0f); // 设置音调
textToSpeech.setSpeechRate(1.0f); // 设置语速
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
isSupport = false;
Log.i(TAG, "系统不支持中文语音播报");
}
}else {
Log.e(TAG, "初始化系统TTS 失败");
}
}
});
}
public static SystemTTS getInstance(Context context) {
if (instance == null) {
instance = new SystemTTS(context);
}
return instance;
}
public void speak(String text) {
if (!isSupport) {
return;
}
if (textToSpeech != null) {
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
public void destroy() {
if (textToSpeech != null) {
textToSpeech.stop();
textToSpeech.shutdown();
}
instance = null;
}
SystemTTS.getInstance(this).speak("测试语音输出");
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)