gpt4 book ai didi

java - Android SeekBar 更新音量按钮点击

转载 作者:行者123 更新时间:2023-12-02 11:40:05 25 4
gpt4 key购买 nike

我正在尝试将 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);
}

最佳答案

我在我的seekBar中使用这个方法

    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,volume,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;

关于java - Android SeekBar 更新音量按钮点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48647993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com