美文网首页
kotlin TTS使用总结

kotlin TTS使用总结

作者: 释校尉 | 来源:发表于2019-10-15 11:08 被阅读0次

package com.ljb.mvp.kotlin.utils

import android.annotation.SuppressLint

import android.content.Context

import android.media.AudioManager

import android.speech.tts.TextToSpeech

import android.util.Log

import android.widget.Toast

import java.util.Locale

/**

* 语音播放的一个单例对象

*/

@Suppress("DEPRECATION")

class TTSUtils private constructor(context: Context) {

    //context对象

    private val mContext: Context = context.applicationContext

    //核心播放对象

    private val textToSpeech: TextToSpeech?

    //是否支持

    private var isSupport = true

    init {

        textToSpeech = TextToSpeech(mContext, TextToSpeech.OnInitListener { i ->

            //textToSpeech的配置

            init(i)

        })

    }

    //textToSpeech的配置

    private fun init(i: Int) {

        if (i == TextToSpeech.SUCCESS) {

            val result = textToSpeech!!.setLanguage(Locale.SIMPLIFIED_CHINESE)

            // 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规

            textToSpeech.setPitch(1.0f) //(这里推荐默认,不然不同手机可能发声不同,并且异常)

            textToSpeech.setSpeechRate(1.5f)

            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {

                //系统不支持中文播报

                isSupport = false

                Log.e("语音",""+isSupport)

            }

        }

    }

    fun isTTsSupport():Boolean{

        return isSupport

    }

    fun play(text: String) {

        if (!isSupport) {

            Toast.makeText(mContext, "暂不支持", Toast.LENGTH_SHORT).show()

            return

        }

        //设置播报语音音量(跟随手机音量调节而改变)

        val myHashAlarm = hashMapOf<String,String>()

        myHashAlarm[TextToSpeech.Engine.KEY_PARAM_STREAM] = AudioManager.STREAM_MUSIC.toString()

        //语音播报

        //QUEUE_ADD:播放完之前的语音任务后才播报本次内容

        //QUEUE_FLUSH:丢弃之前的播报任务,立即播报本次内容

        textToSpeech?.speak(text, TextToSpeech.QUEUE_FLUSH, myHashAlarm)

    }

    fun destroy() {

        textToSpeech?.stop()

        textToSpeech?.shutdown()

    }

    companion object {

        //单例对象

        @SuppressLint("StaticFieldLeak")

        private var singleton: TTSUtils? = null

        fun getInstance(context: Context): TTSUtils {

            if (singleton == null) {

                synchronized(TTSUtils::class.java) {

                    if (singleton == null) {

                        singleton = TTSUtils(context)

                    }

                }

            }

            return this.singleton!!

        }

    }

}

activity中使用方法:

TTSUtils.getInstance(this).play("播放内容")

相关文章

  • kotlin TTS使用总结

    package com.ljb.mvp.kotlin.utils import android.annotatio...

  • TTS(Text To Speech)文字转语音简单实现

    TTS实现方案 实现TTS有大体上有两种方案: 1.使用系统内置的TTS优点:集成简单,免费,google语音引擎...

  • vue播报语音

    使用 speak-tts 使用new Audio,播放离线语音

  • MTK FAQ:如何实现连续的PCM流播放

    实现这类PCM的播放(类似于TTS)思路及samplecode如下: 使用双buffer的机制,TTS使用一个,D...

  • Kotlin使用总结

    后端 Java 项目也可引入 Kotlin 优点:1,无缝引入到现有 Java 项目,只看了半天文档就上手了,并且...

  • Kotlin使用总结

    Kotlin优化了复杂界面大段大段的findViewById代码,取而代之的是导包形式:import kotlin...

  • 灵云TTS(语音合成)

    项目中使用了TTS(语音合成功能)刚开始自己准备使用科大讯飞的TTS SDK 但是公司经过半天调研(省钱)决定使用...

  • kotlin 中的异常

    文章目录 前言 kotlin 异常的简介 kotlin 异常的使用 总结 前言 java 中的异常只要 try/c...

  • Kolin学习笔记

    本人于2018年开始使用kotlin开发项目,现将使用过程中总结出来的笔记形成系列文章,以供大家参考。kotlin...

  • Kotlin基础教程(一)

    很久没有对一段时间的工作学习进行总结,所以想就近一个月使用kotlin编程,进行简单的总结.首先,kotlin真的...

网友评论

      本文标题:kotlin TTS使用总结

      本文链接:https://www.haomeiwen.com/subject/lpgjmctx.html