TextToSpeech:service isn't started? - android

Hey i a creating app which is TextToSpeech functionality. I write code and run but no any
speech is generate. some error display in logcat. here is logcat
04-11 20:21:30.099: VERBOSE/TtsService(481): TtsService.setLanguage(eng, USA, )
04-11 20:21:30.109: INFO/TextToSpeech.java - speak(849): speak text of length 41
04-11 20:21:30.109: ERROR/TextToSpeech.java - speak(849): service isn't started
I don't understand how to solve this...here is my full code.
public class ExamAppearingActivity extends Activity implements OnInitListener
{
private int MY_DATA_CHECK_CODE = 0;
private TextToSpeech tts;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.examquestionscreen);
if (isVoiceEnabled==1)
{
tts = new TextToSpeech(this, this);
final List<ObjectiveWiseQuestion> QuestionWiseProfile1= db.getOneQuestion(examId);
for (final ObjectiveWiseQuestion cn : QuestionWiseProfile1)
{
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
db=new MySQLiteHelper(getBaseContext());
db.getWritableDatabase();
counter=cn.getCounter();
String question="Question is "+cn.getQuestion();
String option1="Option A is "+cn.getOptionA();
String option2="Option B is "+cn.getOptionB();
String option3="Option C is "+cn.getOptionC();
String option4="Option D is "+cn.getOptionD();
tts.speak(question, TextToSpeech.QUEUE_ADD, null);
tts.speak(option1, TextToSpeech.QUEUE_ADD, null);
tts.speak(option2, TextToSpeech.QUEUE_ADD, null);
tts.speak(option3, TextToSpeech.QUEUE_ADD, null);
tts.speak(option4, TextToSpeech.QUEUE_ADD, null);
}
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// success, create the TTS instance
tts = new TextToSpeech(this, this);
}
else {
// missing data, install it
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
//tts.isLanguageAvailable(Locale.INDIA_HINDI);
startActivity(installIntent);
}
}
}
#Override
public void onInit(int status)
{
if (status == TextToSpeech.SUCCESS)
{
// tts.setLanguage(Locale.US);
Locale loc = new Locale ("hi_IN");
tts.setLanguage(loc);
Toast.makeText(ExamAppearingActivity.this,"Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
}
else if (status == TextToSpeech.ERROR)
{
Toast.makeText(ExamAppearingActivity.this, "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
}
}
This code is run only when i add it on button click but i need to start it from
onCreate() method.
Any help is appreciated.

You can't use tts until onInit has been called.
At the moment, you create it and try to use it within the onCreate method, but it won't have finished being initialised by then.
You're also creating tts twice. The one in onActivityResult makes most sense because you're checking it exists first. I'd get rid of the creation in onCreate, and put all of the actual speaking into onInit.

Related

synthesizeToFile failed: not bound to TTS engine , when using TextToSpeech in android

I am using Android TextToSpeech API, I want to save the convert text2speech as a file on the SD-card memory, but I got the error:
synthesizeToFile failed: not bound to TTS engine
My code to use TTS is :
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
tts = new TextToSpeech(this, this);
if(getIntent() != null){
if(getIntent().getExtras()!=null){
String d = getIntent().getExtras().getString("data");
String data[] = d.split("-");
bookName = data[0];
loadPage(data[0], Integer.parseInt(data[1]));
}
}
Log.d("TTS","Data is loaded");
}
else {
Intent installTTSIntent = new Intent();
installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
}
where inside the loadPage() function a call the synthesizeToFile function as below :
String tempDestFile = appTmpPath.getAbsolutePath() +"/"+ fileName;
tts.synthesizeToFile(speakTextTxt, myHashRender, tempDestFile);
You have to wait until after onInit is called before you can call speak, synthesizeToFile etc... put your loadPage method in onInit after checking for success there.

speech recognition gets unknown issue in android

I m trying to make an app in which when a call comes to the phone by using android tts it will speech the contact name of calling person(do you want to attend?). according to the reply of user the call should attend or end. in which i always getting a problem with the speech recognition(showing unknown problem) here i m giving my code, can any one help me to solve the issue.i was struck on this since last few days,any help will be appreciable..
public class myPhoneStateChangeListener extends PhoneStateListener
{
speechcontact clsspcntct = new speechcontact();
#Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
if (state == TelephonyManager.CALL_STATE_RINGING)
{
String phoneNumber = incomingNumber;
String ContactName = objUtility.getContactName2(context,phoneNumber);
mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
mAudioManager.setStreamMute(AudioManager.STREAM_RING, true);
speakWords(ContactName);
}
if(state == TelephonyManager.CALL_STATE_IDLE && ph_state==1)
{
mAudioManager.setStreamMute(AudioManager.STREAM_RING, false);
}
}
#Override
public void onInit(int initStatus)
{
if (initStatus == TextToSpeech.SUCCESS) {
if(myTTS.isLanguageAvailable(Locale.ENGLISH)==TextToSpeech.LANG_AVAILABLE)
myTTS.setLanguage(Locale.ENGLISH);
}
else if (initStatus == TextToSpeech.ERROR) {
Log.d("speech log", "Sorry! Text To Speech failed...");
}
}
public void speakWords(String speech)
{
myTTS.speak("you have call from"+speech+"do you want to attend",TextToSpeech.QUEUE_FLUSH, null);
new Timer().schedule(new TimerTask() {
#Override
public void run()
{
startVoiceRecognitionActivity();
}
}, 5000);
}
public void startVoiceRecognitionActivity()
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo...");
try
{
startActivityForResult(intent, REQUEST_CODE);
}
catch (ActivityNotFoundException a)
{
Toast.makeText(getApplicationContext(),"Ops! Your device doesn't support Speech to Text",Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
{
String spch = data.getStringExtra(RecognizerIntent.EXTRA_RESULTS);
if (spch.contains("Yes"))
{
// do smthing
}
else if(spch.contains("No"))
{
// do smething
}
}
super.onActivityResult(requestCode, resultCode, data);
}
The problem is that you are adding FLAG_ACTIVITY_NEW_TASK to your intent which according to the documentations:
This flag can not be used when the caller is requesting a result from the activity being launched.
and you are using it with startActivityForResult , removing
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
from your code fixes the issue.
I have an app that do what you want, it works fine on 2.3 to 4.0.
On jelly Bean does not work. Google made changes and looks like Jelly Bean stop
the voice recognition during the phone ringing.
So if you are testing on Jelly bean, try your code on another device.

Trying to read barcodes with Zxing, but it seems the onActivityResult is not beeing called

as the title says, I'm trying to scan 1D barcodes, so far I have thet following code:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void test(View view){
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "1D_CODE_MODE");
startActivityForResult(intent, 0);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
IntentResult intentResult =
IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (intentResult != null) {
String contents = intentResult.getContents();
String format = intentResult.getFormatName();
TextView uno = (TextView) findViewById(R.id.textView1);
uno.setText(contents);
Toast.makeText(this, "Numero: " + contents, Toast.LENGTH_LONG).show();
Log.d("SEARCH_EAN", "OK, EAN: " + contents + ", FORMAT: " + format);
} else {
Log.e("SEARCH_EAN", "IntentResult je NULL!");
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.e("SEARCH_EAN", "CANCEL");
}
}
}
}
And of course, I have both IntentResult and IntentIntegrator added to the project.
So, the scanner is beeing called correctly when a button is pressed and it seems to scan the code perfectly (it says "Text found" after it scans it), but it seems that the onActivityResult is not called, since the TextView is not beeing updated and the Toast is not appearing.
Any idea on what the mistake could be?
Thanks in advance!
Your first mistake is not using IntentIntegrator.initiateScan(), replacing it with your own hand-rolled call to startActivityForResult().
Your second mistake is in assuming that IntentIntegrator.REQUEST_CODE is 0. It is not.
Hence, with your current code, you are sending out a request with request code of 0, which is coming back to onActivityResult() with request code of 0, which you are ignoring, because you are only looking for IntentIntegrator.REQUEST_CODE.
Simply replace the body of your test() method with a call to initiateScan(), and you should be in better shape. Here is a sample project that demonstrates the use of IntentIntegrator.
I resolve your same problem so.
public class MainActivity extends Activity {
private TextView tvStatus, tvResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.tvStatus = (TextView) findViewById(R.id.tvStatus);
this.tvResult = (TextView) findViewById(R.id.tvResult);
Button scanBtn = (Button) findViewById(R.id.btnScan);
scanBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_FORMATS", "QR_CODE_MODE");
startActivityForResult(intent,
IntentIntegrator.REQUEST_CODE);
} catch (Exception e) {
Log.e("BARCODE_ERROR", e.getMessage());
}
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(
requestCode, resultCode, intent);
if (scanResult != null) {
this.tvStatus.setText(scanResult.getContents());
this.tvResult.setText(scanResult.getFormatName());
}
}
}
The onActivityResault function must be overridden. just add an #Override before the function declaration and it will be solved.

Text to Speech Android

I am trying to create a Text to Speech app that will remember the last sentence or part of the sentence after a comma where it was when the app is paused. Below is my code.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pitch = (EditText) findViewById(R.id.pitch);
words = (EditText) findViewById(R.id.wordsToSpeak);
words.setText("This message is intended only for the use of the individual or entity to which it is addressed and may contain information that is privileged, confidential or exempt from disclosure by law.");
speakBtn = (Button) findViewById(R.id.speak);
// Check to be sure that TTS exists and is okay to use
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, REQ_TTS_STATUS_CHECK);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQ_TTS_STATUS_CHECK) {
switch (resultCode) {
case TextToSpeech.Engine.CHECK_VOICE_DATA_PASS:
// TTS is up and running
mTts = new TextToSpeech(this, this);
Log.v(TAG, "Pico is installed okay");
break;
case TextToSpeech.Engine.CHECK_VOICE_DATA_BAD_DATA:
case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_DATA:
case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_VOLUME:
// missing data, install it
Log.v(TAG, "Need language stuff: " + resultCode);
Intent installIntent = new Intent();
installIntent
.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
break;
case TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL:
default:
Log.e(TAG, "Got a failure. TTS apparently not available");
}
} else {
// Got something else
}
}
#Override
public void onInit(int status) {
// Now that the TTS engine is ready, we enable the button
if (status == TextToSpeech.SUCCESS) {
speakBtn.setEnabled(true);
mTts.setOnUtteranceCompletedListener(this);
}
}
public void doSpeak(View view) {
mTts.setPitch(new Float(pitch.getText().toString()));
StringTokenizer st = new StringTokenizer(words.getText().toString(),
",.");
while (st.hasMoreTokens()) {
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,
String.valueOf(uttCount++));
mTts.speak(st.nextToken(), TextToSpeech.QUEUE_ADD, params);
}
// mTts.speak(words.getText().toString(), TextToSpeech.QUEUE_ADD, null);
};
#Override
public void onPause() {
super.onPause();
// if we're losing focus, stop talking
if (mTts != null)
mTts.stop();
}
#Override
public void onDestroy() {
super.onDestroy();
mTts.shutdown();
}
#Override
public void onUtteranceCompleted(String utteranceId) {
Log.v(TAG, "Got completed message for uttId: " + utteranceId);
lastUtterance = Integer.parseInt(utteranceId);
}
}
I am able to get get the android to speak and keep track of the token that it last spoke. However, I am not sure how to resume where it last left off when you press the speakBtn. Is there anyway to go back to a certain token within a tokenizer if all the tokens were not successfully read out loud?
How about:
for(int i=0; i<successfulUtterances; ++i)
tokenizer.nextToken();
String nextUnspokenUtterance = tokenizer.nextToken();
If you're asking whether there's a direct way, there isn't. But this way will get rid of all the tokens you don't need and let you carry on.

string input for Android's speech engine

It seems to me that the method "speak" of class TextToSpeech only works in method onInit or onUtteranceCompleted. However, onInit and onUtteranceCompleted don't have any parameter for passing strings.
In the following code, I tried to define a global string arraylist outside the methods and used the arraylist for string input.For some reason , it didn't work out.But the engine did speak "did you sleep well". Any help is appreciated.
public class TTS extends Activity implements OnInitListener,OnUtteranceCompletedListener,Runnable {
ArrayList<String> content=new ArrayList<String>();
int MY_DATA_CHECK_CODE=50;
private TextToSpeech mTts;
public void onCreate(Bundle savedInstanceState) {
content.add("test");
content.add("another test");
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// success, create the TTS instance
mTts = new TextToSpeech(this,this);
} else {
// missing data, install it
Intent installIntent = new Intent();
installIntent.setAction(
TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
public void onInit(int status){
if(status==TextToSpeech.SUCCESS){
mTts.setLanguage(Locale.US);
mTts.setOnUtteranceCompletedListener(this);
String myText1 = "Did you sleep well?";
mTts.speak(myText1, TextToSpeech.QUEUE_FLUSH, null);
for(int i=0;i<content.size();i++){
mTts.speak(content.get(i),TextToSpeech.QUEUE_ADD,null);
}
if(status==TextToSpeech.ERROR){
mTts.shutdown();
}
}
}
I believe some of your code is missing, but FYI it is possible to assign an ID to an utterance via the parameters map, e.g.:
HashMap<String, String> myHashAlarm = new HashMap();
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "ID of First Utterance");
mTts.speak("It was a clear black night", TextToSpeech.QUEUE_ADD, myHashAlarm);
"ID of First Utterance" will be passed to onUtteranceCompleted(String utteranceId)
Please see Using Text-to-Speech.

Categories

Resources