Android app development to play text to speech TTS
Playing text by changing it to speech is called TTS (Text To Speech).
The Android app supports this function as a basic API.
1. TTS?
-. Text To Speech
2. How to use TTS API
2.1 youtube
-. https://youtu.be/UIUiPqyknvQ
2.2 code (Github)
-. https://github.com/pickersoft/texttospeech
3. Development
3.1 Layout XML
<EditText
android:id="@+id/input"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginBottom="150dp"
android:textAlignment="center"
android:text="Hi. My name is picker. Nice to meet you."
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_baseline_record_voice_over_24"
android:layout_marginTop="100dp"
android:onClick="onSpeech"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
3.2 MainActivity.kt
class MainActivity : AppCompatActivity() {
lateinit var mtts:TextToSpeech
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mtts = TextToSpeech(this) {
mtts.language = Locale.ENGLISH
}
}
fun onSpeech(view: View) {
val tv = findViewById<TextView>(R.id.input)
mtts.speak(tv.text, TextToSpeech.QUEUE_FLUSH, null, null)
}
}
3.3 Demo
4. TTS API DOC
TTS API doc |
Synthesizes speech from text for immediate playback or to create a sound file. A TextToSpeech instance can only be used to synthesize text once it has completed its initialization. Implement the TextToSpeech.OnInitListener to be notified of the completion of the initialization. When you are done using the TextToSpeech instance, call the shutdown() method to release the native resources used by the TextToSpeech engine. Apps targeting Android 11 that use text-to-speech should declare TextToSpeech.Engine.INTENT_ACTION_TTS_SERVICE in the queries elements of their manifest: |
Comments
Post a Comment