android - How can I make TextToSpeech to speak a text with max volume and restore original volume after speak end? -
i save current volume both stream_ring
, stream_music
before stts.get().speak(s, texttospeech.queue_add, null)
, hope texttospeech
can speak text max volume, in fact find texttospeech
speak text current volume, seems stts.get().speak
asynchronous.
how can make texttospeech
speak text max volume , restore original volume after speak end? thanks!
public class speechtxt { private static softreference<texttospeech> stts; public static void speakout(final context context, final string s) { final context appcontext = context.getapplicationcontext(); if (stts == null) { stts = new softreference<texttospeech>(new texttospeech(appcontext, new texttospeech.oninitlistener() { @override public void oninit(int status) { if (status == texttospeech.success) { speak(appcontext, s); } else { } } })); } else { speak(appcontext, s); } } private static void speak(context context, string s) { if (stts != null) { switch (stts.get().setlanguage(locale.getdefault())) { case texttospeech.lang_country_available: case texttospeech.lang_country_var_available: case texttospeech.lang_available: { stts.get().setpitch((float) 0.6); stts.get().setspeechrate((float) 0.8); int currentring=publicparfun.getcurrentvol(context, audiomanager.stream_ring); int currentplay=publicparfun.getcurrentvol(context, audiomanager.stream_music); publicparfun.setringvol(context, 0); publicparfun.setplayvol(context,1000000); stts.get().speak(s, texttospeech.queue_add, null); publicparfun.setringvol(context, currentring); publicparfun.setplayvol(context,currentplay); break; } case texttospeech.lang_missing_data: { break; } case texttospeech.lang_not_supported: // not here } } } public static int getcurrentvol(context mycontext,int streamtype){ audiomanager maudiomanager = (audiomanager)mycontext.getsystemservice(context.audio_service); int current = maudiomanager.getstreamvolume( streamtype); return current; } public static void setringvol(context mycontext,int vol){ setvol(mycontext,audiomanager.stream_ring, vol); } public static void setplayvol(context mycontext,int vol){ setvol(mycontext,audiomanager.stream_music, vol); } private static void setvol(context mycontext,int streamtype,int vol){ audiomanager maudiomanager = (audiomanager)mycontext.getsystemservice(context.audio_service); int max = maudiomanager.getstreammaxvolume(streamtype); if (vol>max){ vol=max; } maudiomanager.setstreamvolume(streamtype,vol, 0); }
}
edit
is following code right? fixed original code thinking.
i can't understand why code need add hashmap, think
stts.get().setonutteranceprogresslistener
,stts.get().setonutterancecompletedlistener
don't need hashmap, right?hashmap ttsparameters = new hashmap(); ttsparameters.put(texttospeech.engine.key_param_utterance_id,"stringid");
package bll;
public class speechtxt { private static softreference stts;
public static void speakout(final context context, final string s) { final context appcontext = context.getapplicationcontext(); if (stts == null) { stts = new softreference<texttospeech>(new texttospeech(appcontext, new texttospeech.oninitlistener() { @override public void oninit(int status) { if (status == texttospeech.success) { speak(appcontext, s); } else { } } })); } else { speak(appcontext, s); } } private static void speak(context context, string s) { if (stts != null) { switch (stts.get().setlanguage(locale.getdefault())) { case texttospeech.lang_country_available: case texttospeech.lang_country_var_available: case texttospeech.lang_available: { stts.get().setpitch((float) 0.6); stts.get().setspeechrate((float) 0.8); final int currentring=publicparfun.getcurrentvol(context, audiomanager.stream_ring); final int currentplay=publicparfun.getcurrentvol(context, audiomanager.stream_music); publicparfun.setringvol(context, 0); publicparfun.setplayvol(context,1000000); final context mycontext=context; hashmap<string, string> ttsparameters = new hashmap<string, string>(); ttsparameters.put(texttospeech.engine.key_param_utterance_id,"stringid"); if (android.os.build.version.sdk_int >=15){ stts.get().setonutteranceprogresslistener(new utteranceprogresslistener() { @override public void ondone(string utteranceid) { publicparfun.setringvol(mycontext, currentring); publicparfun.setplayvol(mycontext,currentplay); } @override public void onerror(string utteranceid) { //also, set volume } @override public void onstart(string utteranceid) { // set volume max } }); }else{ stts.get().setonutterancecompletedlistener(new onutterancecompletedlistener() { @override public void onutterancecompleted(string utteranceid) { publicparfun.setringvol(mycontext, currentring); publicparfun.setplayvol(mycontext,currentplay); } }); } stts.get().speak(s, texttospeech.queue_add, ttsparameters); break; } case texttospeech.lang_missing_data: { break; } case texttospeech.lang_not_supported: // not here } } }
}
edit again
i test many times, when stts.get().speak(s, texttospeech.queue_add, ttsparameters) speak short string, stts.get().setonutterancecompletedlistener(new onutterancecompletedlistener() invoked correctly.
but if stts.get().speak(s, texttospeech.queue_add, ttsparameters) speak long string (about 15 seconds), seems stts.get().setonutterancecompletedlistener(new onutterancecompletedlistener() not invoked. why?
stts.get().setonutterancecompletedlistener(new onutterancecompletedlistener() { @override public void onutterancecompleted(string utteranceid) { publicparfun.setringvol(mycontext, currentring); publicparfun.setplayvol(mycontext,currentplay); } });
the problem be, set currentringvolume directly safter started tts. code not wait until speech has ended. setting volume current volume, after speech has finished utteranceprogresslistener (or onutterancecompletetlistener < api 15. depends on api developing):
//first set hashmap tts hashmap<string, string> ttsparameters = new hashmap<string, string>(); ttsparameters.put(texttospeech.engine.key_param_utterance_id,"stringid"); tts.speak("say hello",texttospeech.queue_flush, ttsparameters );
set onutteranceprogresslistener:
int result = tts.setonutteranceprogresslistener(new utteranceprogresslistener() { @override public void ondone(string utteranceid) { //here set volume } @override public void onerror(string utteranceid) { //also, set volume } @override public void onstart(string utteranceid) { // set volume max } });
edit
and api < 15:
tts.setonutterancecompletedlistener(new onutterancecompletedlistener() { @override public void onutterancecompleted(string utteranceid) { //set volume } });
to find out version installed:
if (android.os.build.version.sdk_int >=8){ }
Comments
Post a Comment