Android TTS volume control

吃可爱长大的小学妹 提交于 2019-12-05 02:35:38

问题


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.


回答1:


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).




回答2:


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);
}   



回答3:


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).




回答4:


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);



回答5:


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




回答6:


Nothing above worked for me until I added a call to setLegacyStreamType() as shown below (in Kotlin):

// Requires Lollipop or above
var ttobj: TextToSpeech? = null

val attrs = AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
                .setLegacyStreamType(AudioAttributes.CONTENT_TYPE_SPEECH)
                .build()
ttobj?.setAudioAttributes(attrs)


来源:https://stackoverflow.com/questions/4545874/android-tts-volume-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!