我正在尝试将Seekbar与设备的音量同步,并在用户使用按钮设置音量时更新它。但它似乎并没有保持同步。有时只更新一次并停止。
try
{
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mVolume.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
mVolume.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
mVolume.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();
}
点击音量按钮:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
try{
mVolume.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC) - 1);
} catch (Error e) {
// min value
}
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
try{
mVolume.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC) + 1);
} catch (Error e) {
// max value
}
}
return super.onKeyDown(keyCode, event);
}
答案 0 :(得分:1)
我在seekBar中使用this方法
private boolean setVolumeMultimedia(int volume) {
AudioManager audioManager = (AudioManager) mActivity.getSystemService(Context.AUDIO_SERVICE);
if (audioManager == null) {
NMPLog.e(TAG, "Unexpected null AudioManager. Unable to get/set the volume/mute.");
return false;
}
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, AudioManager.FLAG_PLAY_SOUND);
NMPLog.i(TAG, "Volume of session: " + audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
return true;
}
我传递audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,音量, AudioManager.FLAG_PLAY_SOUND );而不是0. AudioManager.FLAG_PLAY_SOUND是整数4。
您可以设置此标志。
public static final int FLAG_ALLOW_RINGER_MODES = 2;
public static final int FLAG_PLAY_SOUND = 4;
public static final int FLAG_REMOVE_SOUND_AND_VIBRATE = 8;
public static final int FLAG_SHOW_UI = 1;
public static final int FLAG_VIBRATE = 16;