[Android]seekbar控制系统音量

SeekBar介绍

SeekBar是ProgressBar的子类,通过对ProgressBar进行功能性的补充,可以响应用户的点击和拖动事件。接下来将要介绍属性设置、样式设置以及功能设置。并且通过SeekBar来控制系统音量。

SeekBar常用属性介绍

首先在xml当中对SeekBar进行属性的设置

 <SeekBar
    android:id="@+id/seek_bar"
    style="@android:style/Widget.Material.SeekBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="200"
    android:padding="20dp"
    android:progress="0"
    android:visibility="visible"
    android:progressDrawable="@drawable/seek_bar_voice_bg"
    android:thumb="@drawable/seek_bar_voice_circle"/>

其中,android:max是对进度条的最大值进行设定,在这里进行设置之后,还可以通过使用seekBar.setMax()函数进行设置,SeekBar的默认范围是[0,100]。
android:progress显示的是进度条的初始值,其值不能大于设置的最大值,在这里进行设置之后,还可以通过使用seekBar.setProgress()函数进行设置
android:progressDrawable="@drawable/seek_bar_voice_bg",对进度条的样式进行设置
android:thumb="@drawable/seek_bar_voice_circle",对进度条的按钮样式进行设置

SeekBar样式属性设置

首先对进度条样式进行设置,在drawable路径下,设置seek_bar_voice_bg

<?xml version="1.0" encoding="utf-8"?>    
<layer-list
  xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@android:id/background">
      <shape>
          <solid android:color="#65686A" />
      </shape>
  </item>
  <item android:id="@android:id/secondaryProgress">
      <clip>
          <shape>
              <solid android:color="#ff51495e" />
          </shape>
      </clip>
  </item>
  <item android:id="@android:id/progress">
      <clip>
          <shape>
              <solid android:color="#2287FE" />
          </shape>
      </clip>
  </item>
</layer-list>

其中,backgroud是进度条的背景颜色,progress是已经滑过的进度条的颜色,secondaryProgress是还未滑过的进度条的颜色。

接下来对进度条的样式进行设置:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid
    android:color="#FF9A33"
    />
<size
    android:width="20dp"
    android:height="20dp"/>
</shape>

SeekBar功能的设置

初始化设置

am = (AudioManager)getSystemService(AUDIO_SERVICE);
//获取系统最大音量
int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_SYSTEM );
seekBar.setMax(maxVolume);
//获取当前音量
int currentVolume = am.getStreamVolume(AudioManager.STREAM_SYSTEM);
Log.i("init", String.valueOf(currentVolume));
seekBar.setProgress(currentVolume);
textView.setText(""+currentVolume+"/"+maxVolume);

这里有一个问题,在一开始编这段代码的时候,参考网上的代码时发现,并不能改变系统声音进度条的变化,后来发现是AudioManager有很多的音量设置来源,包括:

 /** Used to identify the volume of audio streams for phone calls */
**public** **static** **final** **int** [STREAM_VOICE_CALL](http://androidxref.com/8.0.0_r4/s?refs=STREAM_VOICE_CALL&project=frameworks) = [AudioSystem](http://androidxref.com/8.0.0_r4/s?defs=AudioSystem&project=frameworks).[STREAM_VOICE_CALL](http://androidxref.com/8.0.0_r4/s?refs=STREAM_VOICE_CALL&project=frameworks);
/** Used to identify the volume of audio streams for system sounds */
**public** **static** **final** **int** [STREAM_SYSTEM](http://androidxref.com/8.0.0_r4/s?refs=STREAM_SYSTEM&project=frameworks) = [AudioSystem](http://androidxref.com/8.0.0_r4/s?defs=AudioSystem&project=frameworks).[STREAM_SYSTEM](http://androidxref.com/8.0.0_r4/s?refs=STREAM_SYSTEM&project=frameworks);
/** Used to identify the volume of audio streams for the phone ring */
**public** **static** **final** **int** [STREAM_RING](http://androidxref.com/8.0.0_r4/s?refs=STREAM_RING&project=frameworks) = [AudioSystem](http://androidxref.com/8.0.0_r4/s?defs=AudioSystem&project=frameworks).[STREAM_RING](http://androidxref.com/8.0.0_r4/s?refs=STREAM_RING&project=frameworks);
/** Used to identify the volume of audio streams for music playback */
**public** **static** **final** **int** [STREAM_MUSIC](http://androidxref.com/8.0.0_r4/s?refs=STREAM_MUSIC&project=frameworks) = [AudioSystem](http://androidxref.com/8.0.0_r4/s?defs=AudioSystem&project=frameworks).[STREAM_MUSIC](http://androidxref.com/8.0.0_r4/s?refs=STREAM_MUSIC&project=frameworks);
/** Used to identify the volume of audio streams for alarms */
**public** **static** **final** **int** [STREAM_ALARM](http://androidxref.com/8.0.0_r4/s?refs=STREAM_ALARM&project=frameworks) = [AudioSystem](http://androidxref.com/8.0.0_r4/s?defs=AudioSystem&project=frameworks).[STREAM_ALARM](http://androidxref.com/8.0.0_r4/s?refs=STREAM_ALARM&project=frameworks);
/** Used to identify the volume of audio streams for notifications */
**public** **static** **final** **int** [STREAM_NOTIFICATION](http://androidxref.com/8.0.0_r4/s?refs=STREAM_NOTIFICATION&project=frameworks) = [AudioSystem](http://androidxref.com/8.0.0_r4/s?defs=AudioSystem&project=frameworks).[STREAM_NOTIFICATION](http://androidxref.com/8.0.0_r4/s?refs=STREAM_NOTIFICATION&project=frameworks);
/** **@hide** Used to identify the volume of audio streams for phone calls when connected
 *        to bluetooth */
**public** **static** **final** **int** [STREAM_BLUETOOTH_SCO](http://androidxref.com/8.0.0_r4/s?refs=STREAM_BLUETOOTH_SCO&project=frameworks) = [AudioSystem](http://androidxref.com/8.0.0_r4/s?defs=AudioSystem&project=frameworks).[STREAM_BLUETOOTH_SCO](http://androidxref.com/8.0.0_r4/s?refs=STREAM_BLUETOOTH_SCO&project=frameworks);
/** **@hide** Used to identify the volume of audio streams for enforced system sounds
 *        in certain countries (e.g camera in Japan) */
**public** **static** **final** **int** [STREAM_SYSTEM_ENFORCED](http://androidxref.com/8.0.0_r4/s?refs=STREAM_SYSTEM_ENFORCED&project=frameworks) = [AudioSystem](http://androidxref.com/8.0.0_r4/s?defs=AudioSystem&project=frameworks).[STREAM_SYSTEM_ENFORCED](http://androidxref.com/8.0.0_r4/s?refs=STREAM_SYSTEM_ENFORCED&project=frameworks);
/** Used to identify the volume of audio streams for DTMF Tones */
**public** **static** **final** **int** [STREAM_DTMF](http://androidxref.com/8.0.0_r4/s?refs=STREAM_DTMF&project=frameworks) = [AudioSystem](http://androidxref.com/8.0.0_r4/s?defs=AudioSystem&project=frameworks).[STREAM_DTMF](http://androidxref.com/8.0.0_r4/s?refs=STREAM_DTMF&project=frameworks);
/** **@hide** Used to identify the volume of audio streams exclusively transmitted through the
 *        speaker (TTS) of the device */
**public** **static** **final** **int** [STREAM_TTS](http://androidxref.com/8.0.0_r4/s?refs=STREAM_TTS&project=frameworks) = [AudioSystem](http://androidxref.com/8.0.0_r4/s?defs=AudioSystem&project=frameworks).[STREAM_TTS](http://androidxref.com/8.0.0_r4/s?refs=STREAM_TTS&project=frameworks);
/** Used to identify the volume of audio streams for accessibility prompts */
**public** **static** **final** **int** [STREAM_ACCESSIBILITY]

所以在代码中使用的AudioManager当中的STREAM_SYSTEM来进行的设置音量最大值以及当前值。

响应音量调节

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if(fromUser){
                seekBarProgress = progress;
                am.setStreamVolume(AudioManager.STREAM_SYSTEM , progress, 0);
                int currentVolume = am.getStreamVolume(AudioManager.STREAM_SYSTEM );
                seekBar.setProgress(currentVolume);
            }
            textView.setText(""+progress);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            textView.setText("开始了");
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            textView.setText("停止了,当前值为:"+seekBarProgress);
            if(seekBarProgress == seekBar.getMax()){
                textView.setText("达到最值");
            }
        }
    });

可以看到,SeekBar需要注册监听器来进行进度条值改变后的响应,包括进度条被拖动后的响应public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser),其中fromUser的含义是判断,进度条的拖动是否由用户操作,在这个函数当中设置了系统的声音;后面的两个函数,用来监听是否开始移动以及停止拖动进度条。

监听声音设置更改

通过注册广播接收器,监听手机按键更改的声音,来同步设置进度条的更改。

private class VolumeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("android.media.VOLUME_CHANGED_ACTION")){
            int currentVolume = am.getStreamVolume(AudioManager.STREAM_SYSTEM);
            seekBar.setProgress(currentVolume);
        }
    }
}

这个广播监听器是通过内部类来动态注册的,注册方法是:

receiver = new VolumeReceiver();
IntentFilter filter = new IntentFilter() ;
filter.addAction("android.media.VOLUME_CHANGED_ACTION") ;
this.registerReceiver(receiver, filter) ;

通过设置对VOLUME_CHANGED_ACTION的更改进行监听,可以动态更改进度条。
动态的广播监听器需要在程序退出的时候取消注册,以防内存泄漏。

protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(receiver);
}

运行效果

运行效果
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容