Android TTS 音量控制

发布于 2024-10-09 07:07:14 字数 78 浏览 3 评论 0原文

向 TTS 引擎发送请求时,有什么方法可以控制 TTS 引擎的音量吗?我可以在这里使用 AudioManager 吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

若能看破又如何 2024-10-16 07:07:14

您可以在 TTS talk() 方法中获得此信息,但只能从 API 级别 11 开始。

为了保持向后兼容性,您可以针对更高的 api 级别(具有较低的 min sdk)并使用“@TargetApi(api_level)”装饰器sdk版本检查。

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

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);
}   
谁把谁当真 2024-10-16 07:07:14

speak() 方法现在可以控制

API 级别 21 中添加的

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

TTS 音量参数 KEY_PARAM_VOLUME,用于指定朗读文本时相对于当前流类型音量的语音音量的键。< br>
音量指定为 0 到 1 之间的浮点数,其中 0 表示静音,1 表示最大音量(默认行为)。

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

自控 2024-10-16 07:07:14

是的,正如您的问题所问,您可以使用 AudioManager TTS 音频。

如果您想在代码中设置音量,则需要使用 getStreamVolume()setStreamVolume() 方法。

如果您想让用户控制音量(这可能取决于您的程序设置音量的方式/时间),这个问题 指出您必须在 OnCreate() 期间调用 setVolumeControlStream()

编辑:不,您无法控制 TTS 引擎的 方法(即Speak() 方法)。

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

瀞厅☆埖开 2024-10-16 07:07:14

尝试以下方法来控制设置:

AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
sb2value =am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
am.setStreamVolume(AudioManager.STREAM_MUSIC, sb2value, 0);

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);
预谋 2024-10-16 07:07:14

int iAlertVolume 是 0% 到 100%,用户将其设置为所需的音量。

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

实际音量设置:

am.setStreamVolume(am.STREAM_MUSIC, (iAlertVolume*amS​​treamMusicMaxVol)/100,0 );

末尾的0表示不显示系统音量控制,设置为1即可查看系统控制

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

乖不如嘢 2024-10-16 07:07:14

直到我添加了对 setLegacyStreamType() 的调用(如下所示(在 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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文