在 Android 中更改媒体音量? [英] Change Media volume in Android?

查看:27
本文介绍了在 Android 中更改媒体音量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以更改媒体音量吗?如何?到目前为止我用过这个:

Can I change the media volume? and how? I used this so far:

setVolumeControlStream(AudioManager.STREAM_MUSIC);

但是有一个搜索栏,想改变媒体音量,而不是铃声.

But have a seekbar and want to change the media volume, not ring volume.

那么有人可以告诉我如何在 onCreate() 中更改媒体音量,我稍后再修复搜索栏.

So can someone show me how to just change the media volume at onCreate() and I fix the seekbar later.

推荐答案

正确的使用方法是 setStreamVolume 在您的 AudioManager 上.它可能看起来像这样

The right method to use would be setStreamVolume on your AudioManager. It could looks like this

AudioManager audioManager = 
    (AudioManager)getSystemService(Context.AUDIO_SERVICE);

audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                             [int value],
                             [if desired a flag]);

该标志的一个示例用途是在设置音量时发出蜂鸣声,以便用户可以听到结果.其标志为 AudioManager.FLAG_PLAY_SOUND.

An example use of the flag is to get the beep when setting the volume so the user can hear the outcome. The flag for that would be AudioManager.FLAG_PLAY_SOUND.

如果您不想播放声音但使用当前值显示吐司,您可以使用 AudioManager.FLAG_SHOW_UI.使用必须得到反馈.无论是听觉还是视觉都没有关系.

You could use AudioManager.FLAG_SHOW_UI if you don't want to play a sound but display a toast with the current value. The use has to get a feedback tho. Doesn't matter if it is audible or visual.

要获得给定流的最大有效值,您只需在 AudioManager 上调用 getStreamMaxVolume() 并返回一个整数,该整数表示......以及最大有效值体积值.

To get the maximal valid value for the given stream you just call getStreamMaxVolume() on the AudioManager and get an integer back which represents ... well the maximal valid value for the volume.

这篇关于在 Android 中更改媒体音量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆