Android TTS 文字转语音,中文需第三方插件

TextToSpeech 默认不支持中文,需要第三方应用,这里推荐 “讯飞语记” 安装后,将应用的录音权限设置为允许,然后在系统的TTS设置下选择 “讯飞语记” ,重启使用TTS的应用即可。

一、使用TextToSpeech完成语音播放

public class TTSTextToSpeech implements TextToSpeech.OnInitListener {

    private final static String TAG = TTSTextToSpeech.class.getSimpleName();

    private static TTSTextToSpeech TTS_TEXT_TO_SPEECH;
    private final TextToSpeech mTTS;

    TTSTextToSpeech() {
    	//Utils.getApp() 是全局的application的变量
        mTTS = new TextToSpeech(Utils.getApp(), this);
    }

    public static synchronized TTSTextToSpeech getInstance() {
        if (null == TTS_TEXT_TO_SPEECH) {
            TTS_TEXT_TO_SPEECH = new TTSTextToSpeech();
        }
        return TTS_TEXT_TO_SPEECH;
    }

    /**
     * 播放,队列式,依次播放
     */
    public boolean speak(String text) {
        // 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
//        mTTS.setPitch(1.0f);
//        // 设置语速
//        mTTS.setSpeechRate(1.0f);

        //播放语音
        return TextToSpeech.SUCCESS ==
                mTTS.speak(text,
                        TextToSpeech.QUEUE_ADD,
                        null,
                        UUID.randomUUID().toString());
    }

    /**
     * 停止,并移除所有后续队列
     */
    public boolean stop() {
        return TextToSpeech.SUCCESS == mTTS.stop();
    }

    @Override
    public void onInit(int status) {
        //todo 系统不支持时使用 讯飞语记
        // 判断是否转化成功
        if (status == TextToSpeech.SUCCESS) {
            //默认设定语言为中文,原生的android貌似不支持中文。
            int result = mTTS.isLanguageAvailable(Locale.CHINA);
            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                LogUtils.w(TAG, "不支持中文..." + String.valueOf(result));
                mTTS.setLanguage(Locale.US);
            } else {
                LogUtils.w(TAG, "支持中文..." + String.valueOf(result));
                mTTS.setLanguage(Locale.CHINA);
            }
        }
    }
}

二、log打印类

/**
 * 用途:log打印,debug时才会打印
 * <p>
 * 作者:mjSoftKing
 * 时间:2021/02/02
 */
public class LogUtils {

    private final static boolean DEBUG = BuildConfig.DEBUG;

    public static void e(Object tag, String msg) {
        e(tag.getClass().getSimpleName(), msg);
    }

    public static void e(Object tag, String msg, Throwable tr) {
        e(tag.getClass().getSimpleName(), msg, tr);
    }

    public static void w(Object tag, String msg) {
        w(tag.getClass().getSimpleName(), msg);
    }

    public static void w(Object tag, String msg, Throwable tr) {
        w(tag.getClass().getSimpleName(), msg, tr);
    }

    public static void d(Object tag, String msg) {
        d(tag.getClass().getSimpleName(), msg);
    }

    public static void d(Object tag, String msg, Throwable tr) {
        d(tag.getClass().getSimpleName(), msg, tr);
    }

    public static void i(Object tag, String msg) {
        i(tag.getClass().getSimpleName(), msg);
    }

    public static void i(Object tag, String msg, Throwable tr) {
        i(tag.getClass().getSimpleName(), msg, tr);
    }


    public static void e(String tag, String msg) {
        if (DEBUG) {
            Log.e(tag, msg);
        }
    }

    public static void e(String tag, String msg, Throwable tr) {
        if (DEBUG) {
            Log.e(tag, msg, tr);
        }
    }

    public static void w(String tag, String msg) {
        if (DEBUG) {
            Log.w(tag, msg);
        }
    }

    public static void w(String tag, String msg, Throwable tr) {
        if (DEBUG) {
            Log.w(tag, msg, tr);
        }
    }

    public static void d(String tag, String msg) {
        if (DEBUG) {
            Log.d(tag, msg);
        }
    }

    public static void d(String tag, String msg, Throwable tr) {
        if (DEBUG) {
            Log.d(tag, msg, tr);
        }
    }

    public static void i(String tag, String msg) {
        if (DEBUG) {
            Log.i(tag, msg);
        }
    }

    public static void i(String tag, String msg, Throwable tr) {
        if (DEBUG) {
            Log.i(tag, msg, tr);
        }
    }

}
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智识家园

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值