android音量控制以及硬件同步

 

获取音量并显示

private void setVolume() { mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); Volumebar.setMax(maxVolume); currentVolume = mAudioManager .getStreamVolume(AudioManager.STREAM_MUSIC); Volumebar.setProgress(currentVolume); mVolume_val.setText(currentVolume * 100 / maxVolume + ""); }  

 

音乐播放的时候调音量的时候同步显示音量,思路是在一个handler 里面刷新。

 

public void handleMessage(Message msg) { switch (msg.what) { case PROGRESS_CHANGED: try { if (musicPlayer.isPlaying()) { currentPosition = musicPlayer.getCurrentPosition(); seekBar.setProgress(currentPosition); setVolume(); myHandler .sendEmptyMessageDelayed(PROGRESS_CHANGED, 200);} 

 

 

你可能感兴趣的:(android,service,音乐)