本文主要是介绍Android 自定义SeekBar动态改变 硬件音量大小 实现和音量键的同步,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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;@Overrideprotected 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);//注册IDseekbar_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()) {@Overridepublic 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 {@Overridepublic 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
参考:http://blog.csdn.net/lyjit/article/details/51538951
这篇关于Android 自定义SeekBar动态改变 硬件音量大小 实现和音量键的同步的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!