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("测试语音输出");
相关推荐
6***94151 天前
MySQL 字符串日期格式转换
android·数据库·mysql
p***q781 天前
MySQL——用户管理
android·mysql·adb
g***86691 天前
MySQL - Navicat自动备份MySQL数据
android·数据库·mysql
q***01771 天前
【MySQL】数据类型
android·数据库·mysql
大数据女孩_Aimee1 天前
AndroidAutoOverUsbInteractiveHostTest FAIL
android·测试用例
c***87191 天前
Tomcat10下载安装教程
android·前端·后端
hqk1 天前
鸿蒙 ArkUI 从零到精通:基础语法全解析
android·前端·harmonyos
5***g2981 天前
MySQL 数据库连接池爆满问题排查与解决
android·数据库·mysql
牛奔1 天前
php 8.2 配置安装php-zbarcode扩展
android·开发语言·php
hudawei9961 天前
kotlin协程编译成Android字节码后是什么样的
android·开发语言·kotlin