iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 20
0
Software Development

Kotlin 2018連續開發30天系列 第 20

# Kotlin 2018連續開發30天 day20 語音轉文字

  • 分享至 

  • twitterImage
  •  

今天寫一下Kotlin 語音轉文字
其實很簡單 利用recognizerIntent就可以了

    val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "請說話..."); //語音辨識 Dialog 上要顯示的提示文字

    startActivityForResult(intent, 1);
    

利用recognizerIntent 完後傳回結果

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (requestCode === 1) {
        if (resultCode == RESULT_OK) {
            //把所有辨識的可能結果印出來看一看,第一筆是最 match 的。
            val result = data!!.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
            var all = ""
            for (r in result) {
                all = all + r + "\n"
            }
            et_message.setText(result.get(0))

        }
    }
    

完成

這個功能也可以做成不用跳出畫面 有空再來完成
現在這個功能有點BUG 之後會再修改一下


上一篇
# Kotlin 2018連續開發30天 day19 第三方library Nice-spinner 下拉式選單
下一篇
Kotlin 2018連續開發30天 day 21 串接ptx api 下集
系列文
Kotlin 2018連續開發30天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言