Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create slider to change android volume?

Tags:

android

slider

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!

like image 631
kevdliu Avatar asked Sep 18 '11 03:09

kevdliu


2 Answers

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);
        }
    });
like image 118
Alan Moore Avatar answered Oct 28 '22 12:10

Alan Moore


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/

like image 31
JimsJump Avatar answered Oct 28 '22 13:10

JimsJump