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("测试语音输出");
相关推荐
2501_91590906几秒前
iOS应用从开发到上架App Store的完整发布流程与步骤指南
android·ios·小程序·https·uni-app·webview
狗凯之家源码网32 分钟前
AI 步数修改提交系统源码应用场景与落地指南
android·数据库·人工智能·运动步数
ZHOUPUYU1 小时前
PHP 9.0 前瞻:下一代 PHP 会带来哪些新功能?
android·开发语言·php
JMchen1 小时前
性能优化——让Native代码飞起来
android·c++·性能优化
ZHOUPUYU2 小时前
PHP 8.2 的核心高级特性,包括只读类、类型系统增强
android·性能优化·php
ZHOUPUYU2 小时前
PHP 8.0 高级技术实战:从新特性到性能优化
android·性能优化·php
不爱说话郭德纲2 小时前
从“点点点”到一键出包:我把 uni-app x Android 离线打包做成了脚本
android·前端·uni-app
事圆则缓3 小时前
Android 崩溃优化与应用稳定性全解析:Crash、ANR、OOM 到线上治理
android
ZHOUPUYU3 小时前
PHP 高级技术实战:从原理到高并发架构落地
android·性能优化·php
stevenzqzq3 小时前
mvi的理解
android·compose