Search code examples
androidvolumeseekbar

When I am developing an Android application ,I want to adjust to the volume of Music with a seekbar,how do it?


When I develop an Android application, I want to adjust to the volume of Music with a seekbar. How can this be done? In addition, when I adjust the volume of Music, I don't want to display a Toast.


Solution

  • please check this code......

        SeekBar ring = null;
    AudioManager mgr = null;
        mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
         ring = (SeekBar) findViewById(R.id.seekBar1);
        initBar(ring, AudioManager.STREAM_RING);
    

    please call the function initbar using your seekbar view instance and stream it will set the music volume as you increase or decrease the seekbar...

    private void initBar(SeekBar bar, final int stream) {
        bar.setMax(mgr.getStreamMaxVolume(stream));
        bar.setProgress(mgr.getStreamVolume(stream));
    
        bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            public void onProgressChanged(SeekBar bar, int progress,
                    boolean fromUser) {
                mgr.setStreamVolume(stream, progress,
                        AudioManager.FLAG_PLAY_SOUND);
            }
    
            public void onStartTrackingTouch(SeekBar bar) {
                // no-op
            }
    
            public void onStopTrackingTouch(SeekBar bar) {
                // no-op
            }
        });
    }