seekbar调整声音大小

直接上代码了

1.xml

seekbar调整声音大小_第1张图片

2.activity

public class TestMusicActivity extends Activity {

    private SeekBar SoundseekBar;

    private AudioManager audioManager;

    @Override

    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_music);

        SoundseekBar=(SeekBar)findViewById(R.id.seekBar2);

        audioManager=(AudioManager)getSystemService(AUDIO_SERVICE);//获取音量服务

        int MaxSound=audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);//获取系统音量最大值

        SoundseekBar.setMax(MaxSound);//音量控制Bar的最大值设置为系统音量最大值

        int currentSount=audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);//获取当前音量

        SoundseekBar.setProgress(currentSount);//音量控制Bar的当前值设置为系统音量当前值

        SoundseekBar.setOnSeekBarChangeListener(new SeekBarListener());

    }

class SeekBarListener implements SeekBar.OnSeekBarChangeListener{

        @Override

        public void onProgressChanged(SeekBar seekBar, int progress,

                                      boolean fromUser) {

                // TODO Auto-generated method stub

            if (fromUser) {

                int SeekPosition=seekBar.getProgress();

                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, SeekPosition, 0);

            }

}

@Override

        public void onStartTrackingTouch(SeekBar seekBar) {

// TODO Auto-generated method stub

        }

@Override

        public void onStopTrackingTouch(SeekBar seekBar) {

    // TODO Auto-generated method stub

        }

    }

}

你可能感兴趣的:(seekbar调整声音大小)