好的,所以我让我的Seek栏改变了Volume,那部分正在运行。但是当我改变手机按钮上的音量时,屏幕上的搜索栏保持不变。 问题:当我增加/减少电话按钮的音量时,SeekBar不会逐渐增加。 怎么设置?
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
private SeekBar volumeSeekbar = null;
private AudioManager audioManager = null;
private void initControls()
{
try
{
volumeSeekbar = (SeekBar)findViewById(R.id.seekBar1);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setOnSeekBarChangeListener(new 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();
}
}
答案 0 :(得分:0)
嗯,这是正常的,因为您需要在使用硬件音量按钮时收到通知。
我看到了这类问题的多重答案,似乎这个答案是普遍的答案:
答案 1 :(得分:0)
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();
}
}
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);
}