创建滑块来改变安卓音量?

发布于 2024-12-05 07:06:48 字数 76 浏览 0 评论 0 原文

有人可以给我一个通过滑块更改音量的示例代码吗? 我搜索了,很多教程都需要我创建一个全新的课程。有一个简单的方法吗?

谢谢!

Can someone give me an sample code for changing volume through a slider?
I searched and a lot of tutorials requied me to create a whole new class. Is there an easier way?

Thanks!

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

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

发布评论

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

评论(3

北凤男飞 2024-12-12 07:06:48

将其添加到您的量中,您必须将Seekbar放入布局XML文件中:

    audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    SeekBar volControl = (SeekBar)findViewById(R.id.volbar);
    volControl.setMax(maxVolume);
    volControl.setProgress(curVolume);
    volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onStopTrackingTouch(SeekBar arg0) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar arg0) {
        }

        @Override
        public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
        }
    });

Add this to your OnCreate, you have to put your seekbar into the layout xml file:

    audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    SeekBar volControl = (SeekBar)findViewById(R.id.volbar);
    volControl.setMax(maxVolume);
    volControl.setProgress(curVolume);
    volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onStopTrackingTouch(SeekBar arg0) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar arg0) {
        }

        @Override
        public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
        }
    });
萤火眠眠 2024-12-12 07:06:48

新波士顿的特拉维斯(Travis)在这里对此有一个很棒的视频教程:
http://www.youtube.com/watch?v=8sr2y66y

可以在此处找到教程:

Travis at the New Boston has a great Video Tutorial on this here:
http://www.youtube.com/watch?v=8sr2Y6Aff6Y

Source code for the tutorials can be found here: http://www.mybringback.com/bringers/android/thenewboston-android-series/828/thenewboston-sample-projects/

韶华倾负 2024-12-12 07:06:48

六年后,这个稍作修改的版本在 Android Studio 3.1 中适用于我。 IDE 说我必须声明 AudioManager 最终:

        /* volume slider*/
final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar volControl = (SeekBar)findViewById(R.id.volControl);
volControl.setMax(maxVolume);
volControl.setProgress(curVolume);
volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onStopTrackingTouch(SeekBar arg0) {
    }

    @Override
    public void onStartTrackingTouch(SeekBar arg0) {
    }

    @Override
    public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
    }
});

//end Volume slider

Six years later, this slightly altered version works for me in Android Studio 3.1. The IDE said I had to declare AudioManager final:

        /* volume slider*/
final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar volControl = (SeekBar)findViewById(R.id.volControl);
volControl.setMax(maxVolume);
volControl.setProgress(curVolume);
volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onStopTrackingTouch(SeekBar arg0) {
    }

    @Override
    public void onStartTrackingTouch(SeekBar arg0) {
    }

    @Override
    public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
    }
});

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