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();
}
Related
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);
}
}
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.
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.
When my app is first installed or opened after being destroyed, it works great. If you back out of it and go back in, then the onStart in UtteranceProgressListener never fires after onInit in OnInitListner. When I open the app after backing out, everything else seems to be working. Log shows that onInit is called, but it never progresses to the onStart in the UtteranceProgressListener. How can I make this work?
in my onCreate:
// Implement Text to speech feature
tts = getTts();
// set progress listener to the TTS engine
tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
#Override
public void onStart(String utteranceId) {
Log.d("LOOK AT ME!!!", "ttsUtteranceListener - onStart");
}
#Override
public void onError(String utteranceId) {
Log.d("LOOK AT ME!!!", "ttsUtteranceListener - onError");
}
#Override
public void onDone(String utteranceId) {
Log.d("LOOK AT ME!!!", "ttsUtteranceListener - onDone");
if (processStarted) {
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
Log.d("LOOK AT ME!!!",
"ttsUtteranceListener - speech,startListening");
speech.startListening(intent);
}
});
} else {
...
}
processStarted = false;
}
});
and here is "getTts()":
private TextToSpeech getTts() {
if (tts == null) {
// Implement Text to speech feature
tts = new TextToSpeech(this, new OnInitListener() {
#Override
public void onInit(int status) {
Log.d("LOOK AT ME!!!", "ttsInitListener - onInit");
if (status == TextToSpeech.SUCCESS) {
tts.setLanguage(Locale.getDefault());
} else {
tts = null;
Toast.makeText(MainActivity.this,
"Failed to initialize TTS engine.",
Toast.LENGTH_SHORT).show();
}
}
});
}
return tts;
}
and here are my lifecycle calls (onPause and onResume) :
// --- LIFECYCLE ---
#Override
public void onResume() {
super.onResume();
Log.d("LOOK AT ME!!!", "onResume");
if (tts == null) {
tts = getTts();
}
}
#Override
public void onPause() {
// Stop then shut down the TextToSpeech Engine
if (tts != null) {
tts.stop();
tts.shutdown();
tts = null;
}
super.onPause();
}
I'm trying to get the activity to finish after it's finished speaking but for some reason I cannot fathom it tells me that the setOnUtteranceCompleted not applicable for text to speech. I'm new to android programming so please be gentle :-)
Here's the code...
public class SpeakActivity extends Activity implements OnUtteranceCompletedListener{
Random randnum = new Random();
TextToSpeech tts = null;
private boolean ttsIsInit = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speak);
// Show the Up button in the action bar.
setupActionBar();
startTextToSpeech();
}
void startTextToSpeech(){
final int randint = randnum.nextInt(4);
final String text = ((GlobVars) this.getApplication()).getResponse(randint);
tts = new TextToSpeech(this, new OnInitListener() {
public void onInit(int status) {
tts.setOnUtteranceCompletedListener(this);
if (status == TextToSpeech.SUCCESS) {
ttsIsInit = true;
if (tts.isLanguageAvailable(Locale.ENGLISH) >= 0){
tts.setLanguage(Locale.ENGLISH);
}
tts.setPitch(0.5f);
tts.setSpeechRate(0.5f);
if (tts != null && ttsIsInit) {
Log.d("got ere", "spoken");
tts.speak(text, TextToSpeech.QUEUE_ADD, null);
}
}
}
});
}
// shut down tts to free the TTS resources
#Override
public void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
#Override
public void onUtteranceCompleted(String arg0) {
((GlobVars) this.getApplication()).setListen(true);
this.finish();
}
}
I am ot sure but as per the docs of setOnUtteranceCompletedListener(), you might need to use TextToSpeech.OnUtteranceCompletedListener listener as an argument. I think the way to use the function is as below. Note that use runOnUIThread method in case you want to make any changes to the UI on the call of the onUtteranceCompleted function.
TextToSpeech tts= new TextToSpeech(context, new OnInitListener() {
#Override
public void onInit(int status) {
tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {
#Override
public void onUtteranceCompleted(String utteranceId) {
//Do things here
}
});
}
});
Source of above : Check onUtteranceCompleted does not get called? question.
Hope this helps.