Media Volume controller in DialogPreference
-
30-04-2021 - |
Question
I am trying to control media volume
But always it automatic set to zero after closing dialog.
code :
DialogPreference dp = (DialogPreference)findPreference("mediavolume");
dp.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
SeekBar volumeBar = (SeekBar)findViewById(R.id.seekbar);
final AudioManager manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
volumeBar.setMax(manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeBar.setProgress( manager.getStreamVolume(AudioManager.STREAM_MUSIC));
volumeBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
manager.setStreamVolume(AudioManager.STREAM_MUSIC, progress,manager.FLAG_PLAY_SOUND);
}
});
return false;
}
});
Solution
I'm not entirely sure what you're trying to do here. You have all the important parts, the volumebar.setMax()
, the volumebar.setProgress()
, and the OnSeekBarChangeListener
, but did you intend to put it in a change listener for the whole dialog? This listener is going to return false unless the dialog itself is changed.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow