Android Text to speech conversion - android

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.

Related

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);

using text to speech in an android class

I am new in java and android. I need to use text to speech in a class not in the activity, is it possible? if yes, how can I do it? I just found good tutorials which it was done in an activity.
Thank you!
I know this is rather late but if your still stumped (hopefully not); or for the purposes of anyone else with the same/similar question.
An stand alone class is pretty simple to do.
It needs context and if you want to pass it an message. Pass both in the constructor.
So you end up with something, which looks like this.
public class MyTTS {
private Context context;
private TextToSpeech tts;
private String txt;
private String TAG = MyTTS.class.getSimpleName();
public MyTTS(Context context, String txt) {
this.context = context;
this.txt = txt;
handleSpeech();
}
private void handleSpeech() {
tts = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.ENGLISH);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextTo
Speech.LANG_NOT_SUPPORTED) {
Log.e(TAG, "This Language is not supported");
} else {
saySomeThing();
}
} else {
Log.e(TAG, "Initialization Failed!");
}
}
});
}
private void saySomeThing() {
if((txt != null) && (txt.length() > 0)) {
tts.speak(txt, TextToSpeech.QUEUE_FLUSH, null);
}
else {
tts.shutdown();
}
}
To execute it:
new MyTTS(context, message);

Convert Toast message to text

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);

Finish an activity,get string typed result from a class and use that result to another activity

I'm new in android environment and started a Software development project, so my knowledge is too few in it. I need help in detail.
Problem details:
Project is on ANDROID OCR code source from github Robert m.theis
currently it's outcome is - while i take an image of any written text,it retrieves quite exact output using tesseract engine as text and search in internet.
my work is -
use the text string (digits ) and call to a phone operator.
my project name is automated mobile card recharging system.
so that i took result text from a method getText() class named OcrResult.java and put into my own activity. But i don't know why this don't working in real device.
it builds, run in emulator, but in real device at least it should show messages! But it doesn't.
i also added in manifest.xml file as (angel braces are remover here)
under activity
activity android:name="edu.sfsu.cs.orange.ocr.call.CallManager"
under application
uses-permission android:name="android.permission.CALL_PHONE"
here my code is
package edu.sfsu.cs.orange.ocr.call;
import edu.sfsu.cs.orange.ocr.OcrResult;
import edu.sfsu.cs.orange.ocr.CaptureActivity;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class CallManager extends Activity
{
public static final String preString = "*111*";
public static final String postString = "#";
//to store retrieved digits
String finalString;
//to get text result from ocr result
OcrResult getStringResult = new OcrResult();
String middleString = getStringResult.getText();
//if it fails to scan desired digit,call the process again
CaptureActivity tryProcessAgain = new CaptureActivity();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
public void setString(String x)
{
middleString = x;
}
public String getString( String toBeInserted)
{
if(toBeInserted.length() == 16)
{
int counter = 0;
char [] insertHere = new char[16];
for(int verifier = 0; verifier < 16; verifier ++)
{
insertHere[verifier] = toBeInserted.charAt(verifier);
if(!Character.isDigit(insertHere[verifier]))
{
break;
}
counter ++;
}
if(counter == 16)
{
finalString = preString + toBeInserted + postString;
return finalString;
}
else
{
// #SuppressWarnings("unused")
//Toast toast = Toast.makeText(this, " number scan invalid.....OCR failed. Please try again.", Toast.LENGTH_SHORT);
//toast.show();
return middleString;
}
}
else
{
//#SuppressWarnings("unused")
//Toast toast = Toast.makeText(this, " number scannin invalid...OCR failed. Please try again.", Toast.LENGTH_SHORT);
//toast.show();
return middleString;
}
}
public void CallToOperator(String callMe)
{
Toast toast = Toast.makeText(this,finalString,Toast.LENGTH_SHORT);
toast.show();
//Toast toast1 = Toast.makeText(this,middleString,Toast.LENGTH_SHORT);
//toast1.show();
if(callMe == finalString)
{
try
{
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + finalString)));
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
tryProcessAgain.onShutterButtonPressContinuous();
}
}
}

Failure in resolving TextToSpeech class for Android Development

I'm new to Android development, Eclipse and Java (done mostly .Net and IVR programming up till now), so when I tried to compile and run a sample app I found for TTS on the Droid, I wasn't surprised that I got a runtime error right away. The error is:
dalvikvm Failed resolving com/sample/TTSapp/AndroidTTSapp; interface 4 'android/speech/tts/TextToSpeech$OnInitListner;'
I suppose the OnInitListner method must be in one of the classes that was installed when I installed the Android SDK (release 1.6 R1 I believe), but I'm not sure how to import the associated class module into the current program. I can't find a speech/tts/TextToSpeech directory anywhere on my system. Do I need to download this directory from somewhere? Following is the Java source code for the demo TTS program I'm trying to run:
package com.sample.TTSApp;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.sample.TTSApp.R;
import java.util.Locale;
import java.util.Random;
public class AndroidTTSapp extends Activity implements
TextToSpeech.OnInitListener
{
private static final String TAG = "TextToSpeechDemo";
private TextToSpeech mTts;
private Button mAgainButton;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Initialize text-to-speech. This is an asynchronous operation.
// The OnInitListener (second argument) is called after initialization completes.
// Instantiate TextToSpeech.OnInitListener
mTts = new TextToSpeech(this, this);
mAgainButton = (Button) findViewById(R.id.again_button);
mAgainButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
sayHello();
}
});
}
#Override
public void onDestroy()
{ // Don't forget to shutdown!
if (mTts != null)
{
mTts.stop();
mTts.shutdown();
}
super.onDestroy();
}
// Implements TextToSpeech.OnInitListener.
public void onInit(int status)
{
// status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
if (status == TextToSpeech.SUCCESS)
{
int result = mTts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
{
// Language data is missing or the language is not supported.
Log.e(TAG, "Language is not available.");
}
else
{
// Check the documentation for other possible result codes. For example, the language may be available for the locale
// but not for the specified country and variant.
// The TTS engine has been successfully initialized. Allow the user to press the button for the app to speak again.
mAgainButton.setEnabled(true);
// Greet the user
sayHello();
}
}
else
{
// Initialization failed.
Log.e(TAG, "Could not initialize TextToSpeech.");
}
};
private static final Random RANDOM = new Random();
private static final String[] HELLOS =
{
"Hello World", "This is Text to speech demo by Zahid Shaikh"
};
private void sayHello()
{
// Select a random hello.
int i =0;
int helloLength = HELLOS.length;
String hello = HELLOS[i];
i++;
if(i == helloLength) i =0;
mTts.speak(hello,TextToSpeech.QUEUE_FLUSH,null);
}
}
Thanks in advance for any assitance anyone can give a beginner like myself.
Don Tilley
On device or emulator in */system/tts/lang_pico/* must be tts-lang-files (*.bin).
It's init TTS example:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
........
........
initTTS();
}
private void initTTS() {
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == MY_DATA_CHECK_CODE) {
if(resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
mTts = new TextToSpeech(this, this);
} else {
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS) {
int result = mTts.setLanguage(Locale.US);
if(result == TextToSpeech.LANG_AVAILABLE
|| result == TextToSpeech.LANG_COUNTRY_AVAILABLE) {
mTts.speak("Start system", TextToSpeech.QUEUE_FLUSH, null);
}
}
}

Categories

Resources