is it possible to start a text to speech instance from a service?
i have tried the following but it doesn't seem to work:
package com.example.SmsSpeaker3;
import java.util.Locale;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.widget.Toast;
public class SmsSpeaker3 extends Service implements OnInitListener{
public static 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!");
mtts = new TextToSpeech(getApplicationContext(),this);
//mtts.speak(rv.str, TextToSpeech.QUEUE_FLUSH,null);
//TextSpeaker.mtts.speak(Receiver.str,
TextToSpeech.QUEUE_FLUSH,null);
}
@Override
public void onStart(Intent intent,int startid)
{
Log.d("SpeakerService","Service started successfully!");
mtts = new TextToSpeech(getApplicationContext(),this);
mtts.setLanguage(Locale.ENGLISH);
Log.d("SmsSpeaker3","created mtts successfully");
//TextSpeaker.mtts.setLanguage(Locale.ENGLISH);
//TextSpeaker.mtts.speak(Receiver.str,
TextToSpeech.QUEUE_FLUSH,null);
mtts.speak(Receiver3.str, TextToSpeech.QUEUE_FLUSH,null);
Log.d("SmsSpeaker3","synthesis done successfully");
}
@Override
public void onDestroy(){
if(mtts!=null)
{
mtts.stop();
Toast.makeText(getApplicationContext(),"The service has
been
destroyed!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onInit(int arg0) {
// TODO Auto-generated method stub
}
}
and
package com.example.SmsSpeaker3;
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; // supports both gsm and cdma
import android.util.Log;
import android.widget.Toast;
public class Receiver3 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();
}
context.startService(new Intent(context,SmsSpeaker3.class));
Toast.makeText(context,str,Toast.LENGTH_LONG).show();
}
}
}
in the log i am getting all the messages in log.d but still i am
unable to receive any sound,only the toast is seen
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en