Android TTS volume control

2019-04-02 13:35发布

Is there any way to control volume of TTS engine when sending request to TTS engine? Can I able to use AudioManager here?

Thank You.

5条回答
够拽才男人
2楼-- · 2019-04-02 14:10

Yes, as your question asks, you are able to use AudioManager for TTS audio.

If you want to set volume in your code, you'll want to use the getStreamVolume() and setStreamVolume() methods.

If you want to give the user control over the volume (this may depend on how/when your program sets volume), this question points out that you have to call setVolumeControlStream() during OnCreate().

EDIT: And no, you cannot control volume within the TTS engine's methods (i.e. the Speak() method).

查看更多
Rolldiameter
3楼-- · 2019-04-02 14:10

you can get this in TTS speak() methods, but only starting from API level 11.

To keep backwards compatibility, you could target the higher api level (with lower min sdk) and use a "@TargetApi(api_level)" decorator along with sdk version check.

/**  speak the single word, at a lower volume if possible */
protected void speakOneWord(String text) {
    int apiVer = android.os.Build.VERSION.SDK_INT;
    if (apiVer >= 11){
        speakApi13(text);
    } else {
        // compatibility mode
        HashMap<String, String> params = new HashMap<String, String>();
        mTts.speak(text, TextToSpeech.QUEUE_ADD, params);
    }
}   

/**  speak at a lower volume, for platform >= 13 */
@TargetApi(13)
protected void speakApi13(String text) {
    HashMap<String, String> params = new HashMap<String, String>();
    params.put(TextToSpeech.Engine.KEY_PARAM_VOLUME, "0.1");
    mTts.speak(text, TextToSpeech.QUEUE_ADD, params);
}   
查看更多
Luminary・发光体
4楼-- · 2019-04-02 14:14

speak() method can now control TTS volume

added in API level 21

int speak (CharSequence text, 
                int queueMode, 
                Bundle: KEY_PARAM_VOLUME;..., 
                String utteranceId)

Parameter KEY_PARAM_VOLUME, key to specify the speech volume relative to the current stream type volume used when speaking text.
Volume is specified as a float ranging from 0 to 1 where 0 is silence, and 1 is the maximum volume (the default behavior).

查看更多
做个烂人
5楼-- · 2019-04-02 14:19

Try this to control the settings:

AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
sb2value =am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
am.setStreamVolume(AudioManager.STREAM_MUSIC, sb2value, 0);
查看更多
萌系小妹纸
6楼-- · 2019-04-02 14:19

int iAlertVolume is a 0% to 100%, user sets this to desired volume.

setVolumeControlStream(AudioManager.STREAM_MUSIC);
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int amStreamMusicMaxVol = am.getStreamMaxVolume(am.STREAM_MUSIC);
am.getStreamVolume(am.STREAM_MUSIC);

Actual Volume setting:

am.setStreamVolume(am.STREAM_MUSIC, (iAlertVolume*amStreamMusicMaxVol)/100,0 );

The 0 at the end says don't display the system volume control, set to 1 to see system ctrl

查看更多
登录 后发表回答