2020-03-19

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

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

 

直接看程式碼


strings.xml

 
<resources>
    <string name="app_name">TextToSpeechDemo</string>
    <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>
    <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>
</resources>
 


activity_main.xml
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    tools:context=".MainActivity">


    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/textspeech"
        android:background="@drawable/edit_text_style"
        android:hint="請輸入要朗讀的文字"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginVertical="10dp"
        android:elevation="0dp"
        android:paddingHorizontal="30dp"
        android:text="開始朗讀" />

</LinearLayout>
 


MainActivity.kt
 
import android.os.Bundle
import android.speech.tts.TextToSpeech
import android.speech.tts.TextToSpeech.OnInitListener
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import java.util.*

class MainActivity : AppCompatActivity()
{
    private var tts: TextToSpeech? = null
    private var btn: Button? = null
    private var editText: EditText? = null
    public override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btn = findViewById<view>(R.id.btn) as Button
        editText = findViewById<view>(R.id.et) as EditText
        tts = TextToSpeech(applicationContext, OnInitListener { status -> if (status == TextToSpeech.SUCCESS)
                                                                                        {
                                                                                            val ttsLang = tts!!.setLanguage(Locale.TRADITIONAL_CHINESE)
                                                                                            if (ttsLang == TextToSpeech.LANG_MISSING_DATA || ttsLang == TextToSpeech.LANG_NOT_SUPPORTED)
                                                                                                {
                                                                                                    Log.e("TTS", "The Language is not supported!")
                                                                                                }
                                                                                            else
                                                                                                {
                                                                                                    Log.i("TTS", "Language Supported.")
                                                                                                }
                                                                                            Log.i("TTS", "Initialization success.")
                                                                                        }
                                                                                    else
                                                                                        {
                                                                                            Toast.makeText(applicationContext, "TTS Initialization failed!", Toast.LENGTH_SHORT).show()
                                                                                        }
                                                                        })
        btn!!.setOnClickListener {  val data = editText!!.text.toString()
                                    Log.i("TTS", "button clicked: $data")
                                    tts!!.setPitch(1F)    // 語調(1 為正常;0.5 為低一倍;2 為高一倍) 
                                    tts!!.setSpeechRate(1F)    // 速度(1 為正常;0.5 為慢一倍;2 為快一倍) 
                                    val speechStatus = tts!!.speak(data, TextToSpeech.QUEUE_FLUSH, null)
                                    if (speechStatus == TextToSpeech.ERROR)
                                    {
                                        Log.e("TTS", "Error in converting Text to Speech!")
                                    }
                                }
    }

    public override fun onDestroy()
    {
        super.onDestroy()
        if (tts!= null)
        {
            tts!!.stop()
            tts!!.shutdown()
        }
    }
}
 



測後要點:
* 降低語調 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) 



沒有留言:

張貼留言