android 7.0 tts文字转语音

支持中文的SDK 语音引擎下载

java 复制代码
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;
    }
java 复制代码
SystemTTS.getInstance(this).speak("测试语音输出");
相关推荐
荣月灵的小梅花11 小时前
在Android 9上修改build.fingerprint
android
帅次11 小时前
Compose 入门:@Composable、组合与重组
android·kotlin·gradle·android jetpack·compose·composable
洞见前行11 小时前
APK Signing Block V2 多渠道分包技术原理
android
DandelionR11 小时前
Android SDK安装
android
雪铃儿12 小时前
Flutter Android 热更新:我为什么没用 Shorebird 而是自己造了一个🚀
android·开源
angerdream12 小时前
Android手把手编写儿童手机远程监控App之通知栏消息
android
OCN_Yang14 小时前
能告诉我:你为什么用 MVI 吗?反正我不理解!
android·架构·前端框架
荣月灵的小梅花15 小时前
Android 给广播接收器增加权限(permission)或signature签名权限
android
沐言人生15 小时前
ReactNative 源码分析4——ReactActivity之加载JSBundle
android·react native
砖厂小工17 小时前
Now In Android 精讲 10 - AGENTS.md:写给 AI Agent 的项目说明书
android