更改 Android 中的媒体音量?

发布于 2024-10-01 21:57:42 字数 210 浏览 7 评论 0原文

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

setVolumeControlStream(AudioManager.STREAM_MUSIC);

但是有一个搜索栏并且想要更改媒体音量,而不是铃声音量。

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

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.

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

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

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

发布评论

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

评论(5

长发绾君心 2024-10-08 21:57:42

正确的使用方法是 上的 setStreamVolume音频管理器。它可能看起来像这样 该

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

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

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

如果您不想播放声音但显示具有当前值的 toast,则可以使用 AudioManager.FLAG_SHOW_UI。使用必须得到反馈。是听觉的还是视觉的并不重要。

要获取给定流的最大有效值,您只需在 AudioManager 上调用 getStreamMaxVolume() 并获取一个整数,该整数代表......好吧,体积。

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

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.

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.

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.

留蓝 2024-10-08 21:57:42
private AudioManager audio;

onCreate 内部:

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

覆盖 onKeyDown:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
        audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
        return true;
    default:
        // return false;
        // Update based on @Rene comment below:
        return super.onKeyDown(keyCode, event);
    }
}
private AudioManager audio;

Inside onCreate:

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

Override onKeyDown:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
        audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
        return true;
    default:
        // return false;
        // Update based on @Rene comment below:
        return super.onKeyDown(keyCode, event);
    }
}
毁虫ゝ 2024-10-08 21:57:42

您可以使用以下代码来使用 SeekBar 来处理 Volume:

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

SeekBar sbVolumeBooster = (SeekBar) findViewById(R.id.sbVolumeBooster);
sbVolumeBooster.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)); 
sbVolumeBooster.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));  

sbVolumeBooster.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
{
    @Override
    public void onStopTrackingTouch(SeekBar arg0) 
    {
    }

    @Override
    public void onStartTrackingTouch(SeekBar arg0) 
    {
    }

    @Override
    public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) 
    {
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                progress, 0);  // 0 can also be changed to AudioManager.FLAG_PLAY_SOUND
    }
});

You can use the following code to handle Volume using a SeekBar:

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

SeekBar sbVolumeBooster = (SeekBar) findViewById(R.id.sbVolumeBooster);
sbVolumeBooster.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)); 
sbVolumeBooster.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));  

sbVolumeBooster.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
{
    @Override
    public void onStopTrackingTouch(SeekBar arg0) 
    {
    }

    @Override
    public void onStartTrackingTouch(SeekBar arg0) 
    {
    }

    @Override
    public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) 
    {
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                progress, 0);  // 0 can also be changed to AudioManager.FLAG_PLAY_SOUND
    }
});
小耗子 2024-10-08 21:57:42

在标志中给出 0- 可以避免获得视觉和音频指示符。
当您实现自己的音频栏和指示器并且不希望 Android 添加任何内容时,这很好。

Giving a 0 - in the flags avoids getting a visual and audio indicator .
That's good when you implement your own audio bar and indicator and you don't want android to add anything.

命比纸薄 2024-10-08 21:57:42

使用 adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, flags);

http://developer.android.com/reference/android/media/AudioManager.html#adjustStreamVolume(int, int, int)

Use adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, flags);

http://developer.android.com/reference/android/media/AudioManager.html#adjustStreamVolume(int, int, int)

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