为什么KEYCODE_VOLUME_UP和KEYCODE_VOLUME_DOWN只在环形音量事件中工作,如何在 andr

  • 本文关键字:VOLUME KEYCODE 事件 工作 andr UP DOWN android onkeydown
  • 更新时间 :
  • 英文 :


我想根据物理音量音乐键更新我的搜索栏,但我的代码不起作用! 请帮助我..

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
customSeekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
return true;
} 
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
//Do something
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
int volume_level= am.getStreamVolume(AudioManager.STREAM_MUSIC);
customSeekBar.setProgress(volume_level-1);
am.adjustStreamVolume( AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER,0);
}else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
//Do something
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
int volume_level= am.getStreamVolume(AudioManager.STREAM_MUSIC);
customSeekBar.setProgress(volume_level+1);
am.adjustStreamVolume( AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE,0);
}
return true;
}

请尝试让我知道..

KeyEvent.KEYCODE_VOLUME_DOWN -> {
audioManager?.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI)
return true
}
KeyEvent.KEYCODE_VOLUME_UP -> {
audioManager?.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI)
return true}   

相关内容

  • 没有找到相关文章

最新更新