Android自定义SeekBar并控制声音

先看自定义的seekbar

<SeekBar
                android:id="@+id/soundseekbar"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:maxHeight="5dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:progressDrawable="@drawable/voicebarbg"
                android:thumb="@drawable/voiceslid" />
maxHeight可自己调节,不设这个属性seekbar有可能特别粗,达不到想要的效果。

thumb就是滑块的图片,progressDrawable是背景图片

voicebarbg.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="@drawable/voicebg">     
    </item>
    
    <item android:id="@android:id/secondaryProgress"
        android:drawable="@drawable/voiceprogress">
        
    </item>
    
    <item android:id="@android:id/progress"
        android:drawable="@drawable/voiceprogress">
    </item>
    
</layer-list>
background是背景 。secondaryProgress是第二背景 ,一般和progress进度一样。

也可以参考一下这个,自己定义颜色的

<?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="@drawable/progressbar">     
    </item>
    
    <item android:id="@android:id/secondaryProgress"
        >
        <clip>
            <shape>
                <corners android:radius="0dip" />
                <gradient
                        android:startColor="#800096ff"
                        android:centerColor="#800096ff"
                        android:centerY="0.75"
                        android:endColor="#a00096ff"
                        android:angle="270"
                />
            </shape>
        </clip>
        
    </item>
    
    <item android:id="@android:id/progress"
        >
        <clip>
            <shape>
                <corners android:radius="0dip" />
                <gradient
                        android:startColor="#ff0096ff"
                        android:centerColor="#ff0096ff"
                        android:centerY="0.75"
                        android:endColor="#ff0096ff"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>
    
</layer-list>
嗯这样一个自定义的seekbar就出来了。下面说说seekbar控制声音,获取系统声音管理器
AudioManager  mAudioManager = (AudioManager) PlayVideoFullScreenAct.this.getSystemService(Activity.AUDIO_SERVICE);

最大声音值

int maxSound = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
当前声音值

curSound = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
把最大声音值和当前声音值传给seekbar

soundbar=(SeekBar)findViewById(R.id.soundseekbar);
soundbar.setMax(maxSound);
soundbar.setProgress(curSound);
然后写个seekbar的监听事件就ok了

soundbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			public void onStopTrackingTouch(SeekBar seekBar) {
				
			}
			public void onStartTrackingTouch(SeekBar seekBar) {
				
			}
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				if(fromUser){
					curSound = seekBar.getProgress();
					mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
							curSound, 0);
				}
			}
		});


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值