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.
Related
I am Creating an app for my school project, where it will read the whole text and speak i.e, TTS. Now I want to implement that, when the user press pause then TTS will stop and When user press Play then TTS will start to speak from the line where it was paused. I heard that it can be achieved by using Media player class but how to do that I don't know and how to put the code in the following code
Here is my TTS code that read the text that is input by the user.
public class MainActivity extends AppCompatActivity {
Button button;
EditText editText;
SeekBar seekBar;
TextToSpeech textToSpeech;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
editText = findViewById(R.id.edittext);
seekBar = findViewById(R.id.seekbar);
textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int i) {
if (i == TextToSpeech.SUCCESS){
int lang = textToSpeech.setLanguage(Locale.US);
if (lang == TextToSpeech.LANG_MISSING_DATA || lang == TextToSpeech.LANG_NOT_SUPPORTED){
Toast.makeText(MainActivity.this, "Language is not supported", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "Language Supported", Toast.LENGTH_SHORT).show();
}
}
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String data = editText.getText().toString();
float speed = (float) seekBar.getProgress() / 50 ;
if (speed < 0.1) speed = 0.1f;
textToSpeech.setSpeechRate(speed);
textToSpeech.speak(data,TextToSpeech.QUEUE_FLUSH,null);
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
if (textToSpeech != null){
textToSpeech.stop();
textToSpeech.shutdown();
}
}
}
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 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.
This question already has answers here:
Using text to speech APIs in android application
(3 answers)
Closed 7 years ago.
Im working on text to speech app , i want to set turkish language to be as this:
tts.setLanguage(Locale.TR);
BUT this is not available in android , is it wrong to add this way or there is different way to add turkish language to text to speech .
any help and advice will be appreciated
text to speech code :
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!
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(Locale.US);
// tts.setPitch(5); // set pitch level
// tts.setSpeechRate(2); // set speech speed rate
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);}}
if your device is Turkish, use :
tts.setLanguage(Locale.getDefault());
instead of
int result = tts.setLanguage(Locale.US);
Then EXTRA_LANGUAGE_MODEL value must be: "tr-TR".
I tried and succeeded.(But I also downloaded Turkish lang.pack from samsung market)
try other speech libraries like http://dragonmobile.nuancemobiledeveloper.com/public/Help/DragonMobileSDKReference_Android/SpeechKit_Guide/Basics.html