Reputation: 91
I want to play a text sequence as speech during an incoming call , lowering the ringtone for few seconds. I've sent the string via intent from a broadcast receiver to another class file. The text is displayed if I use it in toast during call but the speech doesn't play. the code for text to speech in my class file is :-
public class callName extends Activity {
String call;
TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
call = extras.getString("sms");
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.US);
}
}
});
Toast.makeText(getBaseContext(), call, Toast.LENGTH_LONG).show();
tts.speak(call, TextToSpeech.QUEUE_FLUSH, null);
}
}
Upvotes: 1
Views: 934
Reputation: 18151
tts has not been initialize when you call speak, you have to move your speak code inside onInit()
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.US);
tts.speak(call, TextToSpeech.QUEUE_FLUSH, null);
}
}
});
Upvotes: 1