Android 自定义SeekBar动态改变硬件音量大小实现和音量键的同步(推荐)
程序员文章站
2024-02-21 22:55:40
1,上图:
2,代码:
mainactivity.java
package com.hero.zhaoq.seekbarchangeddemo;
imp...
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动态改变硬件音量大小实现和音量键的同步(推荐),希望对大家有所帮助