当前位置: 首页 > 工具软件 > TextToSpeech > 使用案例 >

TextToSpeech文字转语音

危飞文
2023-12-01
class SpeechActivity : AppCompatActivity() {

    private var textToSpeech: TextToSpeech? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        var inflate = ActivitySpeechBinding.inflate(layoutInflater)
        setContentView(inflate.root)

        initSpeech()
        inflate.btnClick.setOnClickListener {
            initData()
        }
    }

    private fun initData() {
        speakText(
            "由来称独立,本自号倾城。\n" +
                    "柳叶眉间发,桃花脸上生。\n" +
                    "腕摇金钏响,步转玉环鸣。\n" +
                    "纤腰宜宝袜,红衫艳织成。\n" +
                    "悬知一顾重,别觉舞腰轻。"
        );
    }

    fun initSpeech() {
        textToSpeech = TextToSpeech(
            applicationContext
        ) { i: Int ->
            if (i == TextToSpeech.SUCCESS) {
                textToSpeech?.setLanguage(Locale.CHINA)
 
 类似资料: