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; /** * Android实现 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) ; } /** * 处理音量变化时的界面显示 * @author long */ 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:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ringtone_seekbar_parent" android:layout_width="match_parent" android:layout_height="160px" android:layout_marginTop="0dip" android:layout_weight="1" android:gravity="center_vertical" android:orientation="horizontal" > <SeekBar android:id="@+id/seekbar_video" android:layout_width="match_parent" android:layout_height="25dp" android:max="0" android:progress="0" android:progressDrawable="@drawable/seekbar_progress_layer_list" android:thumb="@mipmap/play_seekbar_icon" android:layout_weight="18"/> </LinearLayout>
seekbar_progress_layer_list.xml
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!--未选中 状态--> <item android:id="@android:id/background" android:drawable="@mipmap/volumn_bg" /> <!--<!–选中状态–>--> <item android:id="@android:id/progress"> <scale android:drawable="@mipmap/volumn_primary" android:scaleWidth="100%" /> </item> </layer-list>
不是你不会只是你不知道而已……..
整个代码实现很简单。 笔者不在赘述。
注: 打开demo,打开歌曲。滑动seekbar即可改音量大小。
maybe你更需要这个:
源码:
https://github.com/229457269/SeekBarChangedVoiceDemo
参考:Android seekbar(自定义)控制音量同步更新
以上所述是小编给大家介绍的Android 自定义SeekBar动态改变硬件音量大小实现和音量键的同步(推荐),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对找一找教程网网站的支持!