Created
July 5, 2018 06:21
-
-
Save leesc22/5f13cc049e84610fe147f21ab7e4b814 to your computer and use it in GitHub Desktop.
Android TextToSpeech in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:gravity="center" | |
android:orientation="vertical" | |
tools:context=".MainActivity"> | |
<EditText | |
android:id="@+id/edittext_input" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:ems="8" | |
android:gravity="center_horizontal" | |
android:hint="Enter a message" /> | |
<Button | |
android:id="@+id/button_speak" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Speak" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android.texttospeech | |
import android.os.Build | |
import android.support.v7.app.AppCompatActivity | |
import android.os.Bundle | |
import android.speech.tts.TextToSpeech | |
import android.util.Log | |
import kotlinx.android.synthetic.main.activity_main.* | |
import java.util.* | |
class MainActivity : AppCompatActivity(), TextToSpeech.OnInitListener { | |
private var tts: TextToSpeech? = null | |
override fun onInit(status: Int) { | |
if (status == TextToSpeech.SUCCESS) { | |
// set UK English as language for tts | |
val result = tts!!.setLanguage(Locale.UK) | |
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { | |
Log.e("TTS", "The Language specified is not supported!") | |
} else { | |
button_speak.isEnabled = true | |
} | |
} else { | |
Log.e("TTS", "Initialization Failed!") | |
} | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
button_speak.isEnabled = false | |
tts = TextToSpeech(this, this) | |
button_speak.setOnClickListener { speakOut() } | |
} | |
private fun speakOut() { | |
var message = edittext_input.text.toString() | |
if (message.isNullOrBlank()) message = "Please enter a message" | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
tts!!.speak(message, TextToSpeech.QUEUE_FLUSH, null, "") | |
} else { | |
@Suppress("DEPRECATION") | |
tts!!.speak(message, TextToSpeech.QUEUE_FLUSH, null) | |
} | |
} | |
override fun onDestroy() { | |
// Shut down TTS | |
if (tts != null) { | |
tts!!.stop() | |
tts!!.shutdown() | |
} | |
super.onDestroy() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks