1,上图:
2,代码:
MainActivity.Java
package com.hero.zhaoq.seekbarchangeddemo; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.database.ContentObserver; import android.media.AudioManager; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.SeekBar; public class MainActivity extends AppCompatActivity { private SeekBar seekbar_video; private ContentObserver mVoiceObserver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);//获取媒体系统服务 seekbar_video= (SeekBar) findViewById(R.id.seekbar_video);//注册ID seekbar_video.setMax(15);//设置最大音量 seekbar_video.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));// 当前的媒体音量 myRegisterReceiver();//注册同步更新的广播 Log.i("lyj_ring", "mVoiceSeekBar max voluem = "+audioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM)); seekbar_video.setonSeekBarChangeListener(new SeekBar.onSeekBarChangeListener() { public void onStopTrackingTouch(SeekBar arg0) { } public void onStartTrackingTouch(SeekBar arg0) { } public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) { AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); Log.v("lyj_ring", "mVoiceSeekBar max progress = "+arg1); //系统音量和媒体音量同时更新 audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, arg1, 0); audioManager.setStreamVolume(3, arg1, 0);// 3 代表 AudioManager.STREAM_MUSIC } }); mVoiceObserver = new ContentObserver(new Handler()) { @Override public void onChange(boolean selfChange) { super.onChange(selfChange); AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); seekbar_video.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM)); //或者你也可以用媒体音量来监听改变,效果都是一样的。 //mVoiceSeekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)); } }; } private void myRegisterReceiver(){ MyVolumeReceiver mVolumeReceiver = new MyVolumeReceiver() ; IntentFilter filter = new IntentFilter() ; filter.addAction("android.media.VOLUME_CHANGED_ACTION") ; registerReceiver(mVolumeReceiver, filter) ; } private class MyVolumeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //如果音量发生变化则更改seekbar的位置 if(intent.getAction().equals("android.media.VOLUME_CHANGED_ACTION")){ AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); int currVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC) ;// 当前的媒体音量 seekbar_video.setProgress(currVolume) ; } } } }
main_activity.xml:
seekbar_progress_layer_list.xml
不是你不会只是你不知道而已……..
整个代码实现很简单。 笔者不在赘述。
注: 打开demo,打开歌曲。滑动seekbar即可改音量大小。
maybe你更需要这个:
源码:
https://github.com/229457269/SeekBarChangedVoiceDemo
参考:Android seekbar(自定义)控制音量同步更新
以上所述是小编给大家介绍的Android 自定义SeekBar动态改变硬件音量大小实现和音量键的同步(推荐),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!