import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;
import java.util.HashMap;
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 SpeakerService extends Service implements OnInitListener, OnUtteranceCompletedListener {
public static TextToSpeech mtts;
float rate = (float) 1.0;
Context context;
SpeakerService SpeakerService;
HashMap<String, String> mHashMap;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
Log.d("SpeakerService", "on create Service created successfully!");
mtts = new TextToSpeech(getApplicationContext(), this);
mtts.setLanguage(Locale.ENGLISH);
}
@Override
public void onStart(Intent intent, int startid) {
Log.d("SpeakerService", "Service started successfully!"+startid);
//new TextToSpeech(getApplicationContext(), this);
/*mtts.speak(StarttabActivity.speetchText,
TextToSpeech.QUEUE_FLUSH, null);*/
mHashMap = new HashMap<String, String>();
// tts.speak("Incoming call from", TextToSpeech.QUEUE_FLUSH, mHashMap);
mHashMap.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, ""+StarttabActivity.counter);
mtts.speak(StarttabActivity.speetchText, TextToSpeech.QUEUE_ADD, mHashMap);
}
@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 status) {
Log.i("status", "status===========" + status);
if (status == TextToSpeech.SUCCESS) {
mtts.setLanguage(Locale.ENGLISH);
mtts.setSpeechRate(rate);
mtts.setOnUtteranceCompletedListener(this);
Log.i("status", "=========Text-To-Speech engine is initialized");
} else if (status == TextToSpeech.ERROR) {
Log
.i("status",
"=========Error occurred while initializing Text-To-Speech engine");
/*
* Toast.makeText(BackgroundProcessforTimecheck.this,
* "Error occurred while initializing Text-To-Speech engine",
* Toast.LENGTH_LONG).show();
*/
}
}
@Override
public void onUtteranceCompleted(String utteranceId) {
// TODO Auto-generated method stub
Log.i("tag======", "===========on finished"+utteranceId);
System.out.println("onUtteranceCompleted============="+utteranceId);
}
}
------------------------------------------------------------------------------------------------------------
In the Activity
1.@Override
public void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
stopService(intent);
}
2.
@Override public void onResume() {
super.onResume();
startService(intent);
registerReceiver(broadcastReceiver, new
IntentFilter(SpeakerService.BROADCAST_ACTION)); }
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;
import java.util.HashMap;
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 SpeakerService extends Service implements OnInitListener, OnUtteranceCompletedListener {
public static TextToSpeech mtts;
float rate = (float) 1.0;
Context context;
SpeakerService SpeakerService;
HashMap<String, String> mHashMap;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
Log.d("SpeakerService", "on create Service created successfully!");
mtts = new TextToSpeech(getApplicationContext(), this);
mtts.setLanguage(Locale.ENGLISH);
}
@Override
public void onStart(Intent intent, int startid) {
Log.d("SpeakerService", "Service started successfully!"+startid);
//new TextToSpeech(getApplicationContext(), this);
/*mtts.speak(StarttabActivity.speetchText,
TextToSpeech.QUEUE_FLUSH, null);*/
mHashMap = new HashMap<String, String>();
// tts.speak("Incoming call from", TextToSpeech.QUEUE_FLUSH, mHashMap);
mHashMap.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, ""+StarttabActivity.counter);
mtts.speak(StarttabActivity.speetchText, TextToSpeech.QUEUE_ADD, mHashMap);
}
@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 status) {
Log.i("status", "status===========" + status);
if (status == TextToSpeech.SUCCESS) {
mtts.setLanguage(Locale.ENGLISH);
mtts.setSpeechRate(rate);
mtts.setOnUtteranceCompletedListener(this);
Log.i("status", "=========Text-To-Speech engine is initialized");
} else if (status == TextToSpeech.ERROR) {
Log
.i("status",
"=========Error occurred while initializing Text-To-Speech engine");
/*
* Toast.makeText(BackgroundProcessforTimecheck.this,
* "Error occurred while initializing Text-To-Speech engine",
* Toast.LENGTH_LONG).show();
*/
}
}
@Override
public void onUtteranceCompleted(String utteranceId) {
// TODO Auto-generated method stub
Log.i("tag======", "===========on finished"+utteranceId);
System.out.println("onUtteranceCompleted============="+utteranceId);
}
}
------------------------------------------------------------------------------------------------------------
In the Activity
1.@Override
public void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
stopService(intent);
}
2.
@Override public void onResume() {
super.onResume();
startService(intent);
registerReceiver(broadcastReceiver, new
IntentFilter(SpeakerService.BROADCAST_ACTION)); }
No comments:
Post a Comment