Convert Toast message to text - android

How can I convert this TOAST message to voice in Android?
Example
Toast.makeText(MainActivity.this, "I am enter code here" +positive[+ position]+ " always", Toast.LENGTH_SHORT).show();

First import the package
import android.speech.tts.TextToSpeech;
Then initialize
private TextToSpeech tts;
tts = new TextToSpeech(this, this);
Finally make a function like this
private void speakOut() {
String text = txtText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
Woops. I forgot, you'll also need to define an onInit function
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!");
}
}
In this example I used an onClickListener to call this function, using a button. Modify it to how you want to call this function when you toast a message.
Just make the String text as your toast message. In the example above txtText was an editText. Modify as per your requirement

TextView wordView = (TextView)view; String wordChosen = (String) wordView.getText(); Toast.makeText(MainActivity.this, "I am " + positive[+position] + " always", Toast.LENGTH_SHORT).show(); //tts.speak("I am" + blank +position+" always", TextToSpeech.QUEUE_FLUSH, null); tts.speak("You chose, you are "+wordChosen+" today and always", TextToSpeech.QUEUE_FLUSH, null);

Related

android Text to Speech of a toast message

I am getting an error with the conversion of the text to speech in android studio. I have initialised the code but it still returns no voice outputs. The code is as follows.
else if((match.contains("yes") || match.contains("yeah")) && defsele) {
//Toast toast = Toast.makeText(getApplicationContext(), "Default selection is done and program is starting", Toast.LENGTH_SHORT);
//toast.show();
defsele=false;
switch (progno) {
case 1:
//Toast toast1 = Toast.makeText(getApplicationContext(),"The default settings for cotton cycle is done",Toast.LENGTH_SHORT);
//toast1.show();
String cotton = "The cotton program is starting with the default values";
tts.speak(cotton, TextToSpeech.QUEUE_FLUSH, null);
soak=true;
soakdef();
break;
The tts.speak gets deprecated and does not function. How can I make this work? The initialisation code is as follows:
tts = new TextToSpeech(this,this);
#Override
public void onInit(int status) {
Log.d("Speech", "OnInit - Status ["+status+"]");
if(status == TextToSpeech.SUCCESS){
Log.d("Speech","Success");
tts.setLanguage(Locale.ENGLISH);
TextToSpeech textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
//your text
String textToSpeechStr = "Hello";
//status is success / 0
if (status == TextToSpeech.SUCCESS) {
//speech starts
textToSpeech.speak(textToSpeechStr, TextToSpeech.QUEUE_FLUSH, null);
}
}
});

Can an Android widget toast be a voice? [duplicate]

How can I convert this TOAST message to voice in Android?
Example
Toast.makeText(MainActivity.this, "I am enter code here" +positive[+ position]+ " always", Toast.LENGTH_SHORT).show();
First import the package
import android.speech.tts.TextToSpeech;
Then initialize
private TextToSpeech tts;
tts = new TextToSpeech(this, this);
Finally make a function like this
private void speakOut() {
String text = txtText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
Woops. I forgot, you'll also need to define an onInit function
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!");
}
}
In this example I used an onClickListener to call this function, using a button. Modify it to how you want to call this function when you toast a message.
Just make the String text as your toast message. In the example above txtText was an editText. Modify as per your requirement
TextView wordView = (TextView)view; String wordChosen = (String) wordView.getText(); Toast.makeText(MainActivity.this, "I am " + positive[+position] + " always", Toast.LENGTH_SHORT).show(); //tts.speak("I am" + blank +position+" always", TextToSpeech.QUEUE_FLUSH, null); tts.speak("You chose, you are "+wordChosen+" today and always", TextToSpeech.QUEUE_FLUSH, null);

Android Text to Speech in Turkish

I have problem with pronouncing Turkish characters using Google tts api. It doesn't say anything when i use Utf8 chars for example "ş","ı","ö". How can i solve it?
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
Locale locale = new Locale("tr", "TR");
int result = tts.setLanguage(locale);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
bCevir.setEnabled(true);
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut() {
String Str = null;
Str="Nasılsın";
tts.speak(Str, TextToSpeech.QUEUE_FLUSH, null);
}
}
The problem was solved. It has nothing to do with us or code. It's because of Google text to speech engine that it is ok after last TTS update.
I found TTS service Turkish support. I tried it, works perfect. Read this article http://www.maxicep.com/forum/konu/svox-tts-motoru-turkce-maxipda.930275/

Android Text to speech conversion

I am trying to write a java code for an android application which would convert my text to speech but i am getting an error in makeText method of Toast. I am new to android so please help me. The error which i am getting is
The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (TexttoSpeech, String,
int)
Here is my code
package com.example.messagereader;
import java.util.Locale;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.widget.Toast;
public class TexttoSpeech {
private TextToSpeech tts;
private void speakOut()
{
String num = null;
String mes = null;
String text ="Message From "+num+"Message Body :"+mes;
if (text.length() == 0)
{
tts.speak("You haven't typed text", TextToSpeech.QUEUE_FLUSH,
null);
}
else
{
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
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(TexttoSpeech.this, "Language not Supported",
Toast.LENGTH_LONG).show();
Log.e("TTS", "Language is not supported");
}
}
else
{
Toast.makeText(this, "TTS Initilization Failed", Toast.LENGTH_LONG).show();
Log.e("TTS", "Initilization Failed");
}
}
}
You need to pass to your TexttoSpeech a Context from your activity. In your code this is a referene to TexttoSpeech, and Toast.makeText requires reference to Context as for its first argument. Activity class derives from Context, so you can pass to your class a this from your Activity.
[edit]
For showing Toast-s you can also you application Context, this means you can also show toasts from Services. To get application Context, call getApplicationContext on Context reference.

Text to speech works on emulator but not on phone

I have gone through almost all the links related to this topic but haven't found any suitable answer. I am trying to build a basic text to speech application. It works perfectly fine on the emulator. But when i run it on my phone, it shows language not supported.
I guess this has something to do with the locale. The default locale set in my phone is en_US
My code is as follows:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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) {
// Method yet to be defined
speakOut();
}
});
}
public void onInit(int status) {
// TODO Auto-generated method stub
// TTS is successfully initialized
if (status == TextToSpeech.SUCCESS) {
// Setting speech language
// System.out.println(Locale.getAvailableLocales());
int result = tts.setLanguage(Locale.ENGLISH);
// int result = tts.setLanguage(Locale.getDefault());
// If your device doesn't support language you set above
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
// Cook simple toast message with message
Toast.makeText(this, "Language not supported",
Toast.LENGTH_LONG).show();
Log.e("TTS", "Language is not supported");
} else {
btnSpeak.setEnabled(true);
}
// TTS is not initialized properly
} else {
Toast.makeText(this, "TTS Initilization Failed", Toast.LENGTH_LONG)
.show();
Log.e("TTS", "Initilization Failed");
}
}
private void speakOut() {
// Get the text typed
String text = txtText.getText().toString();
// If no text is typed, tts will read out 'You haven't typed text'
// else it reads out the text you typed
if (text.length() == 0) {
tts.speak("You haven't typed text", TextToSpeech.QUEUE_FLUSH, null);
} else {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
public void onDestroy() {
// Don't forget to shutdown!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
even if i use int result = tts.setLanguage(Locale.getDefault()); instead of Locale.ENGLISH, it shows the same language not supported message
LOGCAT ERROR:
02-26 16:24:57.492: I/TextToSpeech.java(23356): initTts() successfully bound to service
02-26 16:24:58.015: E/TTS(23356): Language is not supported
I am running this on my phone- Samsung Galaxy-Y (GT-S5360) with Android version 2.3.6
found a solution myself. the problem was that the locales available on the android default tts engine did not match those on my phone.
i installed another tts engine and it works perfectly fine with it.

Categories

Resources