安卓 TextToSpeech: speak failed: not bound to TTS engine


關於語音播報一段時間沒有使用系統返回 speak failed: not bound to TTS engine 錯誤解決辦法

通過textToSpeech?.speak 返回參數判斷播放是否成功如果返回-1需要重新實例化TextToSpeech。

完整代碼:

SystemTTS
package com.dzw.pushlib.audio

import android.content.Context
import android.speech.tts.TextToSpeech
import android.speech.tts.UtteranceProgressListener
import android.util.Log
import java.util.*

class SystemTTS private constructor(private val context: Context) : UtteranceProgressListener(), TTS {

    // 系統語音播報類
    private var textToSpeech: TextToSpeech? = null
    private var isSuccess = false

    init {
        initSpeech()
    }

    /**
     * 初始化語音播報
     */
    private fun initSpeech(onSuccess: () -> Unit = {}) {
        textToSpeech = TextToSpeech(context) { statue: Int ->
            try {
                //系統語音初始化成功
                if (statue == TextToSpeech.SUCCESS) {
                    isSuccess = true
                    val result = textToSpeech!!.setLanguage(Locale.CHINA)
                    textToSpeech!!.setOnUtteranceProgressListener(this)
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        //系統不支持中文播報
                        isSuccess = false
                        return@TextToSpeech
                    }
                    onSuccess()
                } else {
                    isSuccess = false
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    override fun playText(playText: String) {
        // 設置音調,值越大聲音越尖(女生),值越小則變成男聲,1.0是常規
        textToSpeech?.setPitch(1.0f)
        textToSpeech?.setSpeechRate(1.0f)
        val status = textToSpeech?.speak(playText, TextToSpeech.QUEUE_ADD, null, null)
        Log.e("wade", "$status")
        if (status == TextToSpeech.ERROR) {
            initSpeech {
                // 設置音調,值越大聲音越尖(女生),值越小則變成男聲,1.0是常規
                playText(playText)
            }
        }
    }

    override fun stopSpeak() {
        if (textToSpeech != null) {
            textToSpeech?.stop()
        }
    }

    override fun onStart(utteranceId: String) {}
    override fun onDone(utteranceId: String) {}
    override fun onError(utteranceId: String) {}



    companion object {
        private var singleton: SystemTTS? = null

        @JvmStatic
        fun getInstance(context: Context): TTS? {
            if (singleton == null) {
                synchronized(SystemTTS::class.java) {
                    if (singleton == null) {
                        singleton = SystemTTS(context)
                    }
                }
            }
            return singleton
        }
    }
}

TTS

public interface TTS {
    void playText(String playText);

    void stopSpeak();
}

 

使用方法:

SystemTTS.getInstance(context).playText("測試到賬");

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM