2020-03-19

【Kotlin】TextToSpeech -- 請 Google 小姐朗讀

模擬器不支援朗讀中文,只能在實體裝置測試。

 

直接看程式碼


strings.xml

  1.  
  2. <resources>
  3. <string name="app_name">TextToSpeechDemo</string>
  4. <string name="textspeech">Android 提供內容豐富的應用程式架構,可讓您在 Java 語言環境中建置適用於行動裝置的新穎應用程式和遊戲。 With her nine-to-five job, Sally sometimes has to run personal during the lunch break, such as going to the bank or mailing letters.(A) affairs (B) errands (C) belongings (D) connections</string>
  5. <string name="textspeech1">With her nine-to-five job, Sally sometimes has to run personal during the lunch break, such as going to the bank or mailing letters.(A) affairs (B) errands (C) belongings (D) connections</string>
  6. </resources>
  7.  


activity_main.xml
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:gravity="center"
  9. android:orientation="vertical"
  10. android:paddingLeft="20dp"
  11. android:paddingRight="20dp"
  12. tools:context=".MainActivity">
  13.  
  14.  
  15. <EditText
  16. android:id="@+id/et"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:text="@string/textspeech"
  20. android:background="@drawable/edit_text_style"
  21. android:hint="請輸入要朗讀的文字"
  22. android:paddingBottom="10dp"
  23. android:paddingLeft="10dp"
  24. android:paddingRight="10dp"
  25. android:paddingTop="10dp" />
  26.  
  27. <Button
  28. android:id="@+id/btn"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:layout_marginVertical="10dp"
  32. android:elevation="0dp"
  33. android:paddingHorizontal="30dp"
  34. android:text="開始朗讀" />
  35.  
  36. </LinearLayout>
  37.  


MainActivity.kt
  1.  
  2. import android.os.Bundle
  3. import android.speech.tts.TextToSpeech
  4. import android.speech.tts.TextToSpeech.OnInitListener
  5. import android.support.v7.app.AppCompatActivity
  6. import android.util.Log
  7. import android.view.View
  8. import android.widget.Button
  9. import android.widget.EditText
  10. import android.widget.Toast
  11. import java.util.*
  12.  
  13. class MainActivity : AppCompatActivity()
  14. {
  15. private var tts: TextToSpeech? = null
  16. private var btn: Button? = null
  17. private var editText: EditText? = null
  18. public override fun onCreate(savedInstanceState: Bundle?)
  19. {
  20. super.onCreate(savedInstanceState)
  21. setContentView(R.layout.activity_main)
  22. btn = findViewById<view>(R.id.btn) as Button
  23. editText = findViewById<view>(R.id.et) as EditText
  24. tts = TextToSpeech(applicationContext, OnInitListener { status -> if (status == TextToSpeech.SUCCESS)
  25. {
  26. val ttsLang = tts!!.setLanguage(Locale.TRADITIONAL_CHINESE)
  27. if (ttsLang == TextToSpeech.LANG_MISSING_DATA || ttsLang == TextToSpeech.LANG_NOT_SUPPORTED)
  28. {
  29. Log.e("TTS", "The Language is not supported!")
  30. }
  31. else
  32. {
  33. Log.i("TTS", "Language Supported.")
  34. }
  35. Log.i("TTS", "Initialization success.")
  36. }
  37. else
  38. {
  39. Toast.makeText(applicationContext, "TTS Initialization failed!", Toast.LENGTH_SHORT).show()
  40. }
  41. })
  42. btn!!.setOnClickListener { val data = editText!!.text.toString()
  43. Log.i("TTS", "button clicked: $data")
  44. tts!!.setPitch(1F) // 語調(1 為正常;0.5 為低一倍;2 為高一倍)
  45. tts!!.setSpeechRate(1F) // 速度(1 為正常;0.5 為慢一倍;2 為快一倍)
  46. val speechStatus = tts!!.speak(data, TextToSpeech.QUEUE_FLUSH, null)
  47. if (speechStatus == TextToSpeech.ERROR)
  48. {
  49. Log.e("TTS", "Error in converting Text to Speech!")
  50. }
  51. }
  52. }
  53.  
  54. public override fun onDestroy()
  55. {
  56. super.onDestroy()
  57. if (tts!= null)
  58. {
  59. tts!!.stop()
  60. tts!!.shutdown()
  61. }
  62. }
  63. }
  64.  



測後要點:
* 降低語調 tts!!.setPitch(0.5F), 聽起來有點鴨嗓,降至 0.1F,像重感冒的鴨嗓,並不會因降低語調而變成男聲。
* 以老人家的 Zenfone 3 為測試手機,Locale 不論設成 CHINA, CHINESE, TAIWAN, TRADITIONAL_CHINESE, SIMPLIFIED_CHINESE, 都是同樣的聲音語調。
* 由於大陸是漢語拼音,所以當設為 Locale.CHINESE(/TAIWAN/TRADITIONAL_CHINESE/SIMPLIFIED_CHINESE),而且要朗讀的文字中混雜有英文時,Google 小姐會將英文單字轉換為中文發音唸出。
* 但若設為 Locale.CHINA,就不會將英文單字轉為中文發音,雖然腔調怪怪的,但仍能聽得出是英文。
* 若設為英文語系:Locale.ENGLISH(/US/UK),遇到中, 英文混合,某些機種的 Google 小姐會直接略過中文,只唸英文;而有的機種則是洋腔說中文。

 

 

相關筆記 ----

【Kotlin】微軟的 文字轉換語音(Text To Speach, TTS) 



沒有留言:

張貼留言