Android 使用SeekBar调节系统音量

以下是一个使用SeekBar来调节系统音量的实例:
1、XML:
   <SeekBar
  android:id="@+id/sound"
  android:layout_width="150px"
  android:layout_height="10px"
  android:max="100"  //设置拖动条最大值
  android:progress="10"   //设置拖动条当前值
  android:progressDrawable="@layout/seekbar_style"  //拖动条样式
  android:thumb="@layout/thumb" />  //滑块样式
 

2、seekbar_style.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">  
        <shape>  
            <corners android:radius="10dip" />  
            <gradient android:startColor="#ffffffff" 
                android:centerColor="#ff000000" android:endColor="#ff808A87" 
                android:centerY="1" android:angle="270" />  
        </shape>  
    </item>  
 
    <item android:id="@android:id/progress">  
        <clip>  
            <shape>  
                <corners android:radius="10dip" />  
                <gradient android:startColor="#ffffffff" 
                    android:centerColor="#ffFFFF00" android:endColor="#ffAABD00" 
                    android:centerY="1" android:angle="270" />  
            </shape>  
        </clip>  
    </item>  
 </layer-list>   


3、thumb.xml:
 <?xml version="1.0" encoding="UTF-8"?>    
 <selector xmlns:android="http://schemas.android.com/apk/res/android">          
  <!-- 按下状态 -->   
  <item      
   android:state_pressed="true"   
   android:drawable="@drawable/thumb_normal"     
   />     
  <!-- 普通无焦点状态 -->   
  <item      
   android:state_focused="false"      
   android:state_pressed="false"    
   android:drawable="@drawable/thumb_normal" 
  />  
 </selector>   


4、bacon_seekbar.xml:
 <layer-list
   xmlns:android="http://schemas.android.com/apk/res/android">
   <item
  android:id="@+android:id/background"
  android:drawable="@drawable/thumb_normal" />
   <item
  android:id="@+android:id/SecondaryProgress"
  android:drawable="@drawable/thumb_normal" />
   <item
  android:id="@+android:id/progress"
  android:drawable="@drawable/thumb_normal" />
 </layer-list>


5、JAVA代码:
public class PianoActivity extends Activity {
    /** Called when the activity is first created. */
  private ImageButton imageButton_white1;
 private MediaPlayer mediaPlayer01;
 public  AudioManager audiomanage;
 private TextView mVolume ;  //显示当前音量
  public  SeekBar soundBar;
  private int maxVolume, currentVolume; 

 

 private int volume=0;  //初始化声音

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mediaPlayer01 = new MediaPlayer();      
      

        imageButton_white1=(ImageButton)findViewById(R.id.white1);
        final SeekBar soundBar=(SeekBar)findViewById(R.id.sound);  //音量设置
        mVolume = (TextView)findViewById(R.id.mVolume); 
        audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 


        maxVolume = audiomanage.getStreamMaxVolume(AudioManager.STREAM_MUSIC);  //获取系统最大音量
        soundBar.setMax(maxVolume);   //拖动条最高值与系统最大声匹配
        currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM_MUSIC);  //获取当前值
        soundBar.setProgress(currentVolume); 
        mVolume.setText(currentVolume*100/maxVolume + " %"); 
 
        soundBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){调音监听器
          public void onProgressChanged(SeekBar arg0,int progress,boolean fromUser){
                audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);  
                currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM_MUSIC);  //获取当前值
                soundBar.setProgress(currentVolume);  
                mVolume.setText(currentVolume*100/maxVolume + " %");  
          }
           
       @Override
      public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
         }
       @Override
      public void onStopTrackingTouch(SeekBar seekBar) {
           // TODO Auto-generated method stub    

       }
      });

 

转载于:https://www.cnblogs.com/zhangping/p/3514146.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值