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("测试语音输出");
相关推荐
花开彼岸天~13 分钟前
Flutter跨平台开发:Android View 在鸿蒙系统上的使用指南
android·flutter·harmonyos
子一!!15 分钟前
MySQL==表的结构操作1
android·python·adb
你不是我我21 分钟前
【Java 开发日记】我们来说一下 MySQL 的慢查询日志
android·java·mysql
梦幻通灵9 小时前
Mysql字段判空实用技巧
android·数据库·mysql
龘龍龙12 小时前
Python基础(九)
android·开发语言·python
gjc59212 小时前
MySQL隐蔽 BUG:组合条件查询无故返回空集?深度排查与规避方案
android·数据库·mysql·bug
梨落秋霜13 小时前
Python入门篇【元组】
android·数据库·python
zh_xuan13 小时前
kotlin定义函数和变量
android·开发语言·kotlin
Digitally15 小时前
Android 上的联系人备份和恢复:5 种可靠且方便的方法
android
默|笙15 小时前
【Linux】进程控制(3)进程程序替换
android·linux·运维