tags:

views:

42

answers:

4

i have the following pgm which responds with a Toast with an incoming msg and also speaks out the received msg, however there seems to be no speech synthesis in background but i still can see the Toast though, so should i start a service from the onReceive method (don't know if that's possible) here and then in the startService method , write the speak method??

here's one of my pgms:

Receiver

package com.example.TextSpeaker;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class Receiver extends BroadcastReceiver{


public static String str;
@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();
    Log.d("Receiver","Message received successfully");

    SmsMessage[] msgs = null;

    if(bundle!=null)
    {
        // retrive the sms received

        Object[] pdus = (Object[])bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];
        for(int i=0;i<msgs.length;i++)
        {
            str="";
            msgs[i]=SmsMessage.createFromPdu((byte[]) pdus[i]);
            str+="Message From "+msgs[i].getOriginatingAddress()+".   ";
            str+="The message is "+msgs[i].getMessageBody().toString();
            //TextSpeaker.mtts.speak(Receiver.str, TextToSpeech.QUEUE_FLUSH,null);
            //str="";

        }
        Toast.makeText(context,str,Toast.LENGTH_LONG).show();
        TextSpeaker.mtts.speak(Receiver.str, TextToSpeech.QUEUE_FLUSH,null);

    }
}

}

A: 

Your idea about starting the service in onReceive() and handling the speech output in the service should work. I would do the same. But I'm rather new to android programming.

Jerome
A: 

i tried the following but still i can only get the Toast but not the voice, again it's null synthesis shown in logcat :

Receiver

Intent serviceintent = new Intent();
        serviceintent.setAction(".SpeakerService");
     if(TextSpeaker.flag==1)
        {
            context.stopService(serviceintent);
        }
     else
     {
        context.startService(serviceintent);
        Toast.makeText(context,str,Toast.LENGTH_LONG).show();
        TextSpeaker.mtts.speak(Receiver.str, TextToSpeech.QUEUE_FLUSH,null);
     }

where the SpeakerService is

package com.example.TextSpeaker;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.widget.Toast;


public class SpeakerService extends Service {

//Receiver rv = new Receiver();
//TextSpeaker tspker = new TextSpeaker();
//public TextToSpeech mtts;
@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate(){

    Log.d("SpeakerService","Service created successfully!");


}
@Override
public void onStart(Intent intent,int startid)
{
    Log.d("SpeakerService","Service started successfully!");
    Log.d("SpeakerService","Service started successfully!");

    Log.d("SpeakerService","tspker.mtts = " + TextSpeaker.mtts.toString());
    TextSpeaker.mtts.speak(Receiver.str, TextToSpeech.QUEUE_FLUSH,null);
}
@Override
public void onDestroy(){
    if(TextSpeaker.mtts!=null)
    {
        TextSpeaker.mtts.stop();
        Toast.makeText(getApplicationContext(),"The service has been destroyed!", Toast.LENGTH_SHORT).show();
    }

}

}

pranay
actually the service is not getting started ,so how can i correct that?
pranay
i did manage to start the service but still the same log message that "null-synthesis, can't speak" when i try to run the service in background
pranay
A: 
Intent serviceintent = new Intent();
        serviceintent.setAction(".SpeakerService");

i guess using the following change in ur intent might help:

Intent serviceintent = new Intent(context, SpeakerService.class)
JaVadid
thanks it worked! but again, though i am seeing in the log that service started but i can't hear any sound, does the tts engine run in background at all?
pranay
AKAIK yes, the tts engine keeps running in the background... and if its not showing any error in your log, i guess this means its working fine... By the way are you trying to use it on emulator or device??
JaVadid
A: 

You need to give enough time for TTS service to start before you try to use it.

tubsturtle