TTS define Text To speech.
Android provides TextToSpeech functionality from API level 4.
TextToSpeech is in package android.speech.tts.
Here is how to work with text to speech functionality in android.
1. Create a Project.
2. Create an Activity like I created MainActivity.java.
public class MainActivity extends AppCompatActivity
3. Implement interface TextToSpeech.OnInitListener
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener
4. Override TextToSpeech.OnInitListener's methods.
@Override
public void onInit(int status)
5. Create field variable of TextToSpeech
private TextToSpeech tts;
6. Initialize variable with creating new object of TextToSpeech.
tts = new TextToSpeech(this, this);
7. init() will get result if TTS is initialized successfully or not.
8. Check if TTS initialized successfully.
if (status == TextToSpeech.SUCCESS) {}
9. If initialized successfully then set Language to TTS instance.
int result = tts.setLanguage(Locale.US);
10. setLanguage() return int for result that if language is set or not.
11. Check that if given language is set successfully.
if (result != TextToSpeech.LANG_MISSING_DATA
&& result != TextToSpeech.LANG_NOT_SUPPORTED) {}
12. Now your TTS is initialized successfully. Now we can give TTS a text to Speech.
13. Call speech() of tts.
tts.speak("This is Text to Speech Text", TextToSpeech.QUEUE_FLUSH, null);
14. We can modily pinch and speed of reading text also with setPinch() and setSpeechRate().
tts.setPitch(1f);
tts.setSpeechRate(1f);
15 . Default pinch and speed value is 1f.
Happy Coding :D
0 Comment(s)