android tts hindi locale - android

I am building a tts app using the google tts libraries. when I set the language locale to hindi and get back to app it doesn't say the text correctly. I have to restart the app for it to speak hindi correctly.
t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
}
});
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak = ed1.getText().toString().trim();
t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
});

Use below code for hindi tts use. that code work for me.
public class AndroidTextToSpeechActivity extends Activity implements
TextToSpeech.OnInitListener {
private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tts = new TextToSpeech(this, this);
btnSpeak = (Button) findViewById(R.id.btnSpeak);
txtText = (EditText) findViewById(R.id.txtText);
// button on click event
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
speakOut();
}
});
}
#Override
public void onDestroy() {
// Don't forget to shutdown!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
#Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(new Locale("hi"));
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "Language is not supported");
} else {
btnSpeak.setEnabled(true);
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed");
}
}
private void speakOut() {
String text = txtText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}

Related

How to make TextToSpeech language turkish voice

I'm using Android Studio. Code java.I want to make TextToSpeech language Turkish.How can I make the voice Turkish?
my code:
TextToSpeech textToSpeech ;
ImageView ımagespeech;
textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int i) {
if (i !=TextToSpeech.ERROR){
textToSpeech.setLanguage(new Locale("en_US"));
textToSpeech.setSpeechRate((float)1.0);
}
}
});
ımagespeech.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String gettext =tvtcontextt.getText().toString();
textToSpeech.speak(gettext,TextToSpeech.QUEUE_FLUSH,null);
}
});
#Override
protected void onPause() {
if (textToSpeech != null ){
textToSpeech.stop();
textToSpeech.shutdown();
}
super.onPause();
}
I recently discovered this languages. The speech quality looks promising on the web app.
Give it a shot :)
this is my solution,
in this way the code selects the language used in the device:
private TextToSpeech tts;
#Override
public void onResume() {
super.onResume();
//((AppCompatActivity) getActivity()).getSupportActionBar().hide();
tts = new TextToSpeech(requireContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
//int result = tts.setLanguage(Locale.forLanguageTag("en-US"));
int result = tts.setLanguage(Locale.forLanguageTag(Locale.getDefault().toLanguageTag()));
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "The Language not supported!");
} else {
Log.d("TTS", "TTS its working");
}
}
}
});
}
// in your code
tts.speak("read this", TextToSpeech.QUEUE_FLUSH, null, "");
#Override
public void onPause() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onPause();
}

How to prevent a TTS(Text to Speech) to start reading immediately after opening the activity?

I used a tts for reading my text view,but as soon as i open the activity, it start reading, but i don't want it like this, i want to press the button then it start reading, here is the code :
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
private TextToSpeech tts;
private Button buttonSpeak;
private Textview tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
buttonSpeak = (Button) findViewById(R.id.button1);
tv= (Textview) findViewById(R.id.tv1);
buttonSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
speakOut();
} });}
#Override
public void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy(); }
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
buttonSpeak.setEnabled(true);
speakOut(); } } else {
Log.e("TTS", "Initilization Failed!");
}}
private void speakOut() {
String text = Textview.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); }}
Call speakOut(); from onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
buttonSpeak = (Button) findViewById(R.id.button1);
tv= (Textview) findViewById(R.id.tv1);
speakOut();
}
Don't call speakout() from inside onInit().

TTS: Success returned from onInit() but still there is delay for first speak

Code used:
public class TexttoSpeechActivity extends Activity implements OnInitListener {
private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tts = new TextToSpeech(this, this);
btnSpeak = (Button) findViewById(R.id.btnSpeak);
txtText = (EditText) findViewById(R.id.txtText);
btnSpeak.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
speakOut();
}
});
}
#Override
public void onDestroy() {
// Don't forget to shutdown!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(this, "Language not supported", Toast.LENGTH_LONG).show();
Log.e("TTS", "Language is not supported");
} else {
btnSpeak.setEnabled(true);
}
} else {
Log.e("TTS", "Initilization Failed");
}
}
private void speakOut() {
String text = txtText.getText().toString();
if (text.length() == 0) {
tts.speak("You haven't typed text", TextToSpeech.QUEUE_FLUSH, null);
} else {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
System.out.println("apr2 tts isSpeaking "+tts.isSpeaking());
}
}
success status is returned from onInit(). But there is some delay for first speak. i.e. if on launching button is immedialely clicked for speak, tts not working. After some delay(10-15 seconds), it is working fine.
I can use handler for wait 10-15 seconds as described in Text to Speech not working as expected.
But this is not good. Because this 10-15 seconds delay can vary device to device.
Anyone has clue how to handle this first delay.

How to enable Arabic language in my app?

I tried this code which converts text to speech, but I want the speech to be in Arabic language if the text is written in Arabic.
I looked at other qustions related to this topic, but nothing works with me
The layout has Edit text , button, and TextView.
public class AndroidTextToSpeechActivity extends Activity implements
TextToSpeech.OnInitListener {
private int result=0;
private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tts = new TextToSpeech(this, this);
btnSpeak = (Button)findViewById(R.id.btnSpeak);
txtText = (EditText)findViewById(R.id.txtText);
//button on click event
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
speakOut();
}
});
}
//shutdown tts when activity destroy
#Override
public void onDestroy() {
// Don't forget to shutdown!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
//It will called before TTS started
#Override
public void onInit(int status) {
/
if (status == TextToSpeech.SUCCESS) {
result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(this, "Missing data", Toast.LENGTH_LONG).show();
btnSpeak.setEnabled(false);
} else {
btnSpeak.setEnabled(true);
}
} else {
Log.e("TTS", "Initilization Failed");
}
}
//call this method to speak text
private void speakOut() {
String text = txtText.getText().toString();
if(result!=tts.setLanguage(Locale.US))
{
Toast.makeText(getApplicationContext(), "Enter right Words...... ", Toast.LENGTH_LONG).show();
}else
{
//speak given text
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
}
Well, you do set the tts language to Locale.US, so that's why it speaks English. Provided your tts engine has Arabic voice data you could set the locale to "ar" instead using the setLanguage method you already use.
It's not clear however if you also need to automatically determine which language the text is written in, that's a whole different story.

How to initiate TTS in OnCreate method in android

I want to Convert some text to Speech during oncreate method.
That is when the activity starts it will speak some text.
How can i do that???
I know how to work normally with tts.
These are sample code. But it doesn't work when the activity starts.
public class AndroidTextToSpeechActivity extends Activity implements
TextToSpeech.OnInitListener {
/** Called when the activity is first created. */
private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tts = new TextToSpeech(this, this);
btnSpeak = (Button) findViewById(R.id.btnSpeak);
txtText = (EditText) findViewById(R.id.txtText);
// button on click event
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
speakOut();
}
});
}
#Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
btnSpeak.setEnabled(true);
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut() {
String text = txtText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
I had the same problem. I solved it by implementing the tts service a little bit different:
in onCreate:
TextToSpeech tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int i) {
methodSpeek();
}
}, "com.google.android.tts");
This way your text only starts after it is initialized.
Your code is playing dynamically generated speech as soon as it possibly can, because you are calling
speakOut() in the onInit() method, which is the callback that fires when the text-to-speech synthesizer is ready to use.
If you want to generate speech even sooner, and you know ahead of time the phrase to speak and the locale in which to speak it, you can pregenerate synthetic speech, save it into a WAV file, and play it back later with MediaPlayer:
HashMap<String, String> myHashRender = new HashMap();
String wakeUpText = "Are you up yet?";
String destFileName = "/sdcard/myAppCache/wakeUp.wav";
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, wakeUpText);
mTts.synthesizeToFile(wakuUpText, myHashRender, destFileName);
See the article for details.

Categories

Resources