Android technology sharing - text-to-speech and read aloud
I'm working on a project recently, and there is a function that needs to convert text into speech and play it out. Below I will share my approach.
It is very pleasing that the Android
system is now integrated TTS
and provides relevant libraries for us to call, so we don't have to search for third-party libraries everywhere, just import them directly android.speech.tts.TextToSpeech
.
//import the package of TTS import android.speech.tts.TextToSpeech; //Define a tts object private TextToSpeech tts;
Second, to instantiate this object requires two parameters, one is the Context
object, and the other is TextToSpeech
the listener object corresponding to the class: the OnLnitListener
object. The general Context
object is passed into the current one Activity
, and OnLnitListener
you can write your own class inheritance and implement its methods.
//Import the listener package import android.speech.tts.TextToSpeech.OnInitListener; //Initialize the tts listener object tts = new TextToSpeech(this, OnInitListener);
OnLnitListener
As long as there is a onInit
method in the interface, its function is to tts
initialize the object, set the language, and determine whether the text is successfully converted and whether the current system supports the language.
@Override public void onInit(int status){ // Determine if the conversion is successful if (status == TextToSpeech.SUCCESS){ //The default language is Chinese, the native android does not seem to support Chinese. int result = tts.setLanguage(Locale.CHINESE); if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){ Toast.makeText(MainActivity.this, R.string.notAvailable, Toast.LENGTH_SHORT).show(); }else{ //Set the language to English if Chinese is not supported tts.setLanguage(Locale.US); } } }
Finally, just call the tts-to-text-to-speech method when appropriate.
tts.speak("Text to be converted", TextToSpeech.QUEUE_FLUSH, null);
The following is a demo written:
MainActivity.java:
/** * Author: sandy * QQ technical exchange group: 439261058 * WeChat Official Account: Between Codes (codestravel) **/ package com.example.ct_text2speechdemo; import java.util.Locale; import android.os.Bundle; import android.app.Activity; import android.speech.tts.TextToSpeech; import android.speech.tts.TextToSpeech.OnInitListener; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements OnInitListener{ //Define the control private Button speechButton; private TextView speechText; private TextToSpeech tts; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); //Initialize TTS tts = new TextToSpeech(this, this); //get the control speechText = (TextView)findViewById(R.id.speechTextView); speechButton = (Button)findViewById(R.id.speechButton); //Add listener for button speechButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ // TODO Auto-generated method stub tts.speak(speechText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null); } }); } @Override public void onInit(int status){ // Determine if the conversion is successful if (status == TextToSpeech.SUCCESS){ //The default language is Chinese, the native android does not seem to support Chinese. int result = tts.setLanguage(Locale.CHINESE); if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){ Toast.makeText(MainActivity.this, R.string.notAvailable, Toast.LENGTH_SHORT).show(); }else{ //Set the language to English if Chinese is not supported tts.setLanguage(Locale.US); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
For the complete code, see https://github.com/codestravel/CT_Android_demos/tree/master/CT_Text2SpeechDemo
Welcome to join the qq technical exchange group: 439261058
WeChat Official Account: Between Codes (codetravel)