Android 这个TTS功能有什么问题

Android 这个TTS功能有什么问题,android,android-tts,Android,Android Tts,在我的Android应用程序中,我创建了TTS(文本到语音)功能,如下所示: package hasan.tts_mobile import android.content.Context import android.speech.tts.TextToSpeech import android.speech.tts.UtteranceProgressListener import android.util.Log import java.util.Locale class TtsSpea

在我的Android应用程序中,我创建了TTS(文本到语音)功能,如下所示:

package hasan.tts_mobile

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

import java.util.Locale

class TtsSpeaker(context: Context, private val listener: Listener) : TextToSpeech.OnInitListener {
    private var isInitialized = false
    private var ttsEngine: TextToSpeech? = null

    interface Listener {
        fun onTtsInitialized()
        fun onTtsSpoken()
    }

    init {
        ttsEngine = TextToSpeech(context, this)
    }

    override fun onInit(status: Int) {
        if (status == TextToSpeech.SUCCESS) {
            ttsEngine!!.language = Locale.US
            ttsEngine!!.setOnUtteranceProgressListener(object : UtteranceProgressListener() {

                override fun onStart(utteranceId: String) {
                    Log.i(TAG, "onStart")
                }

                override fun onDone(utteranceId: String) {
                    Log.i(TAG, "onDone")
                    listener.onTtsSpoken()
                }

                override fun onError(utteranceId: String, errorCode: Int) {
                    Log.w(TAG, "onError ($utteranceId). Error code: $errorCode")
                }

                override fun onError(utteranceId: String) {
                    Log.w(TAG, "onError")
                }
            })

            ttsEngine!!.setPitch(1f)
            ttsEngine!!.setSpeechRate(1f)

            isInitialized = true
            Log.i(TAG, "TTS initialized successfully")
            listener.onTtsInitialized()
        } else {
            Log.w(TAG, "Could not open TTS Engine (onInit status=$status). Ignoring text to speech")
            ttsEngine = null
        }
    }

    fun say(message: String) {
        if (!isInitialized || ttsEngine == null) {
            Log.w(TAG, "TTS is not initialized yet, be patient")
            return
        }

        ttsEngine!!.speak(message, TextToSpeech.QUEUE_ADD, null, UTTERANCE_ID)
    }

    fun onDestroy() {
        if (ttsEngine != null) {
            ttsEngine!!.stop()
            ttsEngine!!.shutdown()
        }
    }

    companion object {
        private val TAG = TtsSpeaker::class.java.simpleName
        private const val UTTERANCE_ID = BuildConfig.APPLICATION_ID + ".UTTERANCE_ID"
    }
}

尝试将其称为:

class MainActivity : AppCompatActivity(), TtsSpeaker.Listener {
    var tts: TtsSpeaker? = null
    private var state: State? = null

    private enum class State {
        INITIALIZING,
        LISTENING_TO_KEYPHRASE,
        CONFIRMING_KEYPHRASE,
        LISTENING_TO_ACTION,
        CONFIRMING_ACTION,
        TIMEOUT
    }

        override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if (!isPermissionGranted(permission.RECORD_AUDIO)) {
            requestAudioPermission(this)
        } else {
            Toast.makeText(
                this@MainActivity, "Audio permission is granted",
                Toast.LENGTH_SHORT
            ).show()
            tts = TtsSpeaker(this, this)
        }

    }

    override fun onSpeechRecognizerReady() {
        state = State.INITIALIZING
        tts!!.say("I'm ready!")
    }

        override fun onTtsSpoken() {
        when (state) {
            State.INITIALIZING, State.CONFIRMING_ACTION, State.TIMEOUT -> {
                state = State.LISTENING_TO_KEYPHRASE
                pocketsphinx!!.startListeningToActivationPhrase()
            }
            State.CONFIRMING_KEYPHRASE -> {
                state = State.LISTENING_TO_ACTION
                pocketsphinx!!.startListeningToAction()
            }
            State.LISTENING_TO_KEYPHRASE -> TODO()
            State.LISTENING_TO_ACTION -> TODO()
            null -> TODO()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        tts!!.onDestroy()
    }
}

但它失败了,什么也没说

日志中有什么你可以分享的吗?日志中有什么你可以分享的吗?