I have this seek bar that when I enter the app it takes the value of the phone's volume (if the volume is to the max, the seek bar is full and so on). When I change seek bar value the phone's volume changes (as it should).
But my problem is how to change the seek bar's value when I change the phone's volume through the volume up and volume down buttons in the device
Here's my code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.activity_media_player);
initializeUIElements();
initializeMediaPlayer();
}
private void initializeUIElements() {
sbPlay = (ProgressBar) findViewById(R.id.sbPlay);
sbPlay.setMax(100);
sbPlay.setVisibility(View.INVISIBLE);
btPlay = (Button) findViewById(R.id.btPlay);
btPlay.setOnClickListener(this);
btStop = (Button) findViewById(R.id.btStop);
btStop.setEnabled(false);
btStop.setOnClickListener(this);
sbVolume = (SeekBar) findViewById(R.id.sbVolume);
try {
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
sbVolume.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
sbVolume.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
sbVolume.setOnSeekBarChangeListener(new SeekBar.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);
}
});
}
catch (Exception e) {
e.printStackTrace();
}
}