aspose file tools*
The moose likes Android and the fly likes Speak(read) a text when app is launched Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Speak(read) a text when app is launched " Watch "Speak(read) a text when app is launched " New topic
Author

Speak(read) a text when app is launched

sid shettar
Greenhorn

Joined: Dec 17, 2011
Posts: 7
I am trying to create a mobile app, when the app is launched it should speak some words(Say for example Welome to this app... etc etc). but somehow i am unable to come up with a solution. Can someone please help me in this?

package com.example.helloworld;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.widget.Toast;

public class HelloAndroid extends Activity implements OnInitListener
{
TextToSpeech myTTS;
private static final int MY_DATA_CHECK_CODE = 1;


/** Called when the activity is first created. */
@Override

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//check for TTS data
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);


}

@Override
protected void onStart() {
super.onStart();
// The activity is about to become visible.
String words = "Hi hi hi hi hi hi hi hi hi hi hi";
speakWords(words);

}

//speak the user text
private void speakWords(String speech) {
myTTS.setLanguage(Locale.US);

//speak straight away
myTTS.speak("Hello welcome to this app......", TextToSpeech.QUEUE_FLUSH, null);
}

//act on result of TTS data check
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
//the user has the necessary data - create the TTS
myTTS = new TextToSpeech(this, this);
}
else {
//no data - install it now
Intent installTTSIntent = new Intent();
installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
}
//setup TTS
public void onInit(int initStatus) {

//check for successful instantiation

}
}




Thanks in advance for your help
sid shettar
Greenhorn

Joined: Dec 17, 2011
Posts: 7
I get the below error when i run my app.


12-17 12:34:02.414: W/dalvikvm(392): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
12-17 12:34:02.424: E/AndroidRuntime(392): Uncaught handler: thread main exiting due to uncaught exception
12-17 12:34:02.454: E/AndroidRuntime(392): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld/com.example.helloworld.HelloAndroid}: java.lang.NullPointerException
12-17 12:34:02.454: E/AndroidRuntime(392): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
12-17 12:34:02.454: E/AndroidRuntime(392): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-17 12:34:02.454: E/AndroidRuntime(392): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-17 12:34:02.454: E/AndroidRuntime(392): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-17 12:34:02.454: E/AndroidRuntime(392): at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 12:34:02.454: E/AndroidRuntime(392): at android.os.Looper.loop(Looper.java:123)
12-17 12:34:02.454: E/AndroidRuntime(392): at android.app.ActivityThread.main(ActivityThread.java:4363)
12-17 12:34:02.454: E/AndroidRuntime(392): at java.lang.reflect.Method.invokeNative(Native Method)
12-17 12:34:02.454: E/AndroidRuntime(392): at java.lang.reflect.Method.invoke(Method.java:521)
12-17 12:34:02.454: E/AndroidRuntime(392): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-17 12:34:02.454: E/AndroidRuntime(392): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-17 12:34:02.454: E/AndroidRuntime(392): at dalvik.system.NativeStart.main(Native Method)
12-17 12:34:02.454: E/AndroidRuntime(392): Caused by: java.lang.NullPointerException
12-17 12:34:02.454: E/AndroidRuntime(392): at com.example.helloworld.HelloAndroid.speakWords(HelloAndroid.java:43)
12-17 12:34:02.454: E/AndroidRuntime(392): at com.example.helloworld.HelloAndroid.onStart(HelloAndroid.java:37)
12-17 12:34:02.454: E/AndroidRuntime(392): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
12-17 12:34:02.454: E/AndroidRuntime(392): at android.app.Activity.performStart(Activity.java:3723)
12-17 12:34:02.454: E/AndroidRuntime(392): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
12-17 12:34:02.454: E/AndroidRuntime(392): ... 11 more
12-17 12:34:02.484: I/dalvikvm(392): threadid=7: reacting to signal 3
12-17 12:34:02.644: I/dalvikvm(392): Wrote stack trace to '/data/anr/traces.txt'
12-17 12:34:07.995: I/Process(392): Sending signal. PID: 392 SIG: 9
12-17 12:35:31.255: D/AndroidRuntime(418): Shutting down VM
12-17 12:35:31.255: W/dalvikvm(418): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
12-17 12:35:31.267: E/AndroidRuntime(418): Uncaught handler: thread main exiting due to uncaught exception
12-17 12:35:31.285: E/AndroidRuntime(418): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld/com.example.helloworld.HelloAndroid}: java.lang.NullPointerException
12-17 12:35:31.285: E/AndroidRuntime(418): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
12-17 12:35:31.285: E/AndroidRuntime(418): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-17 12:35:31.285: E/AndroidRuntime(418): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-17 12:35:31.285: E/AndroidRuntime(418): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-17 12:35:31.285: E/AndroidRuntime(418): at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 12:35:31.285: E/AndroidRuntime(418): at android.os.Looper.loop(Looper.java:123)
12-17 12:35:31.285: E/AndroidRuntime(418): at android.app.ActivityThread.main(ActivityThread.java:4363)
12-17 12:35:31.285: E/AndroidRuntime(418): at java.lang.reflect.Method.invokeNative(Native Method)
12-17 12:35:31.285: E/AndroidRuntime(418): at java.lang.reflect.Method.invoke(Method.java:521)
12-17 12:35:31.285: E/AndroidRuntime(418): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-17 12:35:31.285: E/AndroidRuntime(418): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-17 12:35:31.285: E/AndroidRuntime(418): at dalvik.system.NativeStart.main(Native Method)
12-17 12:35:31.285: E/AndroidRuntime(418): Caused by: java.lang.NullPointerException
12-17 12:35:31.285: E/AndroidRuntime(418): at com.example.helloworld.HelloAndroid.speakWords(HelloAndroid.java:43)
12-17 12:35:31.285: E/AndroidRuntime(418): at com.example.helloworld.HelloAndroid.onStart(HelloAndroid.java:37)
12-17 12:35:31.285: E/AndroidRuntime(418): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
12-17 12:35:31.285: E/AndroidRuntime(418): at android.app.Activity.performStart(Activity.java:3723)
12-17 12:35:31.285: E/AndroidRuntime(418): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
12-17 12:35:31.285: E/AndroidRuntime(418): ... 11 more
12-17 12:35:31.325: I/dalvikvm(418): threadid=7: reacting to signal 3
12-17 12:35:31.447: I/dalvikvm(418): Wrote stack trace to '/data/anr/traces.txt'
12-17 12:35:35.284: I/Process(418): Sending signal. PID: 418 SIG: 9
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Caused by: java.lang.NullPointerException at
com.example.helloworld.HelloAndroid.speakWords(HelloAndroid.java:43)

So which line is line 43? Which object is null?
sid shettar
Greenhorn

Joined: Dec 17, 2011
Posts: 7
Hi Tim,

Line number 43 is the method speakWords

" private void speakWords(String speech) { "

Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
I doubt that, since there's no method call in that line. Once you have determined which one is the actual line, the more important question is:
Which object is null?

From looking at the code it is already clear which object is null, but it's good practice for you to figure that out yourself.
sid shettar
Greenhorn

Joined: Dec 17, 2011
Posts: 7
I tried my best but unable to identify it
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
What, exactly, have you done trying to identify which object is null?
sid shettar
Greenhorn

Joined: Dec 17, 2011
Posts: 7
I tried all the possible ways i understood. I am stuck at this point from 4-5 days. Thanks in advance for your help. I am just trying for a simple solution. When an activity is launched its should speak out some words related to that activity without any user interaction
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Again:
Tim Moores wrote:What, exactly, have you done trying to identify which object is null?

If you don't tell us what you did trying to make progress with this issue, then we won't be able to suggest what else you might try. How about this: do you know what a NullPointerException is? And what causes it?

Also: Are you aware of the concept of writing certain information to a log file during developing and/or debugging an application? If yes, are you familiar with the android.util.Log class and the logcat command of the ADB tool?
sid shettar
Greenhorn

Joined: Dec 17, 2011
Posts: 7
Hi Tim,
Yeah, i know about nullpointers. Here in my code the the line "myTTS.speak("Hello welcome to this app......", TextToSpeech.QUEUE_FLUSH, null); " is causing this Null exception which means the object "MyTTS" is pointing to Null because its not being initialized.

But what i could not figure out is, why is it not being initialized because as per my understanding it should had been initialized in the method "onActivityResult" (in my above code).

I know my code is not a good one as I'm just newbie to android here. I have a strong feeling that i am supposed. but my code works as per my expectation when i do this way. but i need to understand how to make my app speak when its started using the start method and also speak when its resumed using resume method. Iam unable to figure out a solution although its the very basic stuff....

package com.example.helloworld;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.widget.Toast;

public class HelloAndroid extends Activity implements OnInitListener
{
private TextToSpeech myTTS;
//private static final int MY_DATA_CHECK_CODE = 0;


/** Called when the activity is first created. */
@Override

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);

myTTS = new TextToSpeech(this, this);


}



//setup TTS
public void onInit(int initStatus) {

//check for successful instantiation
if (initStatus == TextToSpeech.SUCCESS) {
if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
myTTS.setLanguage(Locale.US);

myTTS.speak("", TextToSpeech.QUEUE_FLUSH, null);
}
else if (initStatus == TextToSpeech.ERROR) {
Toast.makeText(this, "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).show();
}
}



}

sid shettar
Greenhorn

Joined: Dec 17, 2011
Posts: 7
thank you Tim Moores
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Speak(read) a text when app is launched