如何控制Android媒体播放器中音量按钮的搜索栏?我需要在播放期间控制音量按钮的搜索栏的onClick方法。
此处提供了代码:
private void initControls() {
// Return the handle to a system-level service - 'AUDIO'.
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// Find the seekbar 1
mediaVlmSeekBar = (SeekBar) findViewById(R.id.seekBar1);
// Set the max range(Volume in this case) of seekbar
// for Media player volume
mediaVlmSeekBar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
// Set the progress with current Media Volume
mediaVlmSeekBar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
try {
// Listener to receive changes to the SeekBar1's progress level
mediaVlmSeekBar
.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar arg0) {
}
public void onStartTrackingTouch(SeekBar arg0) {
}
// When progress level of seekbar1 is changed
public void onProgressChanged(SeekBar arg0,
int progress, boolean arg2) {
audioManager.setStreamVolume(
AudioManager.STREAM_MUSIC, progress, 0);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
//change seekbar when click device button
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
{
mediaVlmSeekBar = (SeekBar) findViewById(R.id.seekBar1);
int index = mediaVlmSeekBar.getProgress();
mediaVlmSeekBar.setProgress(index + 1);
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
int index = mediaVlmSeekBar.getProgress();
mediaVlmSeekBar.setProgress(index - 1);
return true;
}
return super.onKeyDown(keyCode, event);
}
答案 0 :(得分:1)
我已经修改了你的代码并且工作正常。看看
public boolean onKeyDown(int keyCode, KeyEvent event) {
AudioManager audio = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
seekBarVolume.setProgress(currentVolume);
return false;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
seekBarVolume.setProgress(currentVolume);
return false;
}
return super.onKeyDown(keyCode, event);
}