Text to speech(TTS)-Android
Text to speech is built into Android 1.6+. Here is a simple example of how to do it.
TextToSpeech tts = new TextToSpeech(this, this);
tts.setLanguage(Locale.US);
tts.speak("Text to say aloud", TextToSpeech.QUEUE_ADD, null);
More info: http://android-developers.blogspot.com/2009/09/introduction-to-text-to-speech-in.html
Here are instructions on how to download sample code from the Android SDK Manager:
Launch the Android SDK Manager.
a. On Windows, double-click the SDK Manager.exe file at the root of the Android SDK directory.
b. On Mac or Linux, open a terminal to the tools/ directory in the Android SDK, then execute android sdk.
Expand the list of packages for the latest Android platform.
- Select and download Samples for SDK.
When the download is complete, you can find the source code for all samples at this location:
/sdk/samples/android-version/
(i.e. \android-sdk-windows\samples\android-16\ApiDemos\src\com\example\android\apis\app\TextToSpeechActivity.java)
How to create Android text to speech application that says hello world without using text field to write the text?
Your mainactivity:-
package com.example.tts;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Locale;
import android.widget.Toast;
public class MainActivity extends Activity {
TextToSpeech t1;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
t1.setLanguage(Locale.UK);
}
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String toSpeak = "hello world";
Toast.makeText(getApplicationContext(), toSpeak,Toast.LENGTH_SHORT).show();
t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
});
}
public void onPause(){
if(t1 !=null){
t1.stop();
t1.shutdown();
}
super.onPause();
}
}
Why does Text To Speech (TTS) not work all the time?
few hints:
In your if (status == TextToSpeech.ERROR)
, calling ttsString(0);
will always fail, because TTS is not yet initialized.
You seem to not call tts.shutdown(). Actually I would initialize tts in onStart(), and call shutdown() in onStop().
In my app I have put TTS into service, so to not bother with all the initialization/deinitialization while my activity/fragment is recreated during config changes.
Look into logcat, you should see there any errors. I have seen strange errors when using svox voices, as far as I remember the problem was from not calling properly shutdown.
Android - TTS no voice coming out
It seems like this happened because the phone was attached to computer in USB debugging mode. Once disabled, TTS works perfectly!
Android TTS in-text controls? Are they available or any equivalent technique?
The Android TTS engine has the deprecated playSilence()
and the newer playSilentUtterance()
methods that can be used to pause the speech output for a given amount of time.
If the app targets API level 21 i.e. Android 5.0 as the minimum, then playSilentUtterance()
should be used. Otherwise the deprecated playSilence()
is still available.
The complete method signature of the playSilentUtterance
method is:
int playSilentUtterance (long durationInMs, int queueMode, String utteranceId)
Here durationInMs
is the duration of the silence in milliseconds.
The queueMode
can be either QUEUE_ADD
which means that the silence is played after the TTS engine has finished what it is currently speaking and what was already added to the queue and QUEUE_FLUSH
stops everything first and clears the queue, so the silence is played right away.
Finally the utteranceId
is an optional unique identifier for the text (or in this case silence) to be spoken and is useful if using an UtteranceProgressListener.
Android Text to speech plays without error but does not produce any sound
Try removing:
ttsHashMap.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));
Text To Speech (TTS) delays by 3 seconds to speak text
This is probably not the most efficient way of doing it,I had a similar problem to this and used a Handler to solve it.
onCreate(){
textToSpeech = new TextToSpeech(getApplicationContext(), new
TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
textToSpeech.setLanguage(Locale.UK);
}
}
});
textToSpeech.speak("Hello World", TextToSpeech.QUEUE_FLUSH, null, null);
checkIfTTSIsSpeaking();
}
checkIfTTSIsSpeaking() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(textToSpeech.isSpeaking()){
performAction();
}else{
checkIfTTSIsSpeaking();
}
}
},10);
}
performAction(){…}
TextToSpeech engine has a method isSpeaking()
which returns a boolean whether or not the word is being spoken.
TextToSpeech reference
Related Topics
Why Do I Want to Avoid Non-Default Constructors in Fragments
Android Oncreate or Onstartcommand for Starting Service
Android - Get Time of Chronometer Widget
Starting the Android Emulator in Sdk Tools, Revision 12
App Not Run in Visual Studio Emulator for Android in Visual Studio 2015
Can't Get Values Out of Ondatachange Method
Add Padding on View Programmatically
Fragment in Viewpager Using Fragmentpageradapter Is Blank the Second Time It Is Viewed
How to Draw Rounded Rectangle in Android Ui
How to Display a List of Images in a Listview in Android
Android - Custom UI with Custom Attributes
Tabhost with Fragments and Fragmentactivity
Cancel Notification on Remove Application from Multitask Panel
How to Change Textcolor in Alertdialog
Clicking Hamburger Icon on Toolbar Does Not Open Navigation Drawer
How to Insert a SQLite Record with a Datetime Set to 'Now' in Android Application