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("测试语音输出");
相关推荐
zzywxc7871 小时前
AI 行业应用:金融、医疗、教育、制造业领域的落地案例与技术实现
android·前端·人工智能·chrome·金融·rxjava
sTone873751 小时前
android studio之外使用NDK编译生成android指定架构的动态库
android·c++
胖虎12 小时前
Android 入门到实战(三):ViewPager及ViewPager2多页面布局
android·viewpager·viewpager2
风往哪边走4 小时前
Media3在线本地视频播放器
android
激昂网络4 小时前
android kernel代码 common-android13-5.15 下载 编译
android·大数据·elasticsearch
Monkey-旭4 小时前
Android 人脸识别技术全解析
android·android 人脸识别·ml kit 实战·活体检测技术·人脸识别性能优化·人脸考勤系统·移动端人脸特征提取
vivo互联网技术4 小时前
桌面挂件不能承受之重——GIF
android·gif加载·桌面挂件
JulyYu5 小时前
Android系统保存重名文件后引发的异常解决
android·操作系统·源码
叽哥5 小时前
Kotlin学习第 2 课:Kotlin 基础语法:掌握变量、数据类型与运算符
android·kotlin·app