How can I make TextToSpeech to speak a text with max volume and restore original volume after speak end?

Posted by HelloCW on Stack Overflow See other posts from Stack Overflow or by HelloCW
Published on 2014-05-30T07:30:23Z Indexed on 2014/05/30 9:26 UTC
Read the original article Hit count: 448

Filed under:

I save the current volume both STREAM_RING and STREAM_MUSIC before sTts.get().speak(s, TextToSpeech.QUEUE_ADD, null), I hope the TextToSpeech can speak a text with max volume, but in fact I find the TextToSpeech speak the text with current volume, it seems that sTts.get().speak is asynchronous.

How can I make TextToSpeech to speak a text with max volume and 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 much to do 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);
}

}

© Stack Overflow or respective owner

Related posts about android