我正在尝试控制媒体音量
但总是在关闭对话框后自动设置为零。
代码 :
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;
}
});