日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 使用SeekBar调节系统音量

發(fā)布時(shí)間:2023/12/10 Android 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 使用SeekBar调节系统音量 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

以下是一個(gè)使用SeekBar來調(diào)節(jié)系統(tǒng)音量的實(shí)例:
1、XML:
?? <SeekBar
??android:id="@+id/sound"
??android:layout_width="150px"
??android:layout_height="10px"
??android:max="100"? //設(shè)置拖動(dòng)條最大值
??android:progress="10"?? //設(shè)置拖動(dòng)條當(dāng)前值
??android:progressDrawable="@layout/seekbar_style"? //拖動(dòng)條樣式
??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">??????????
??<!-- 按下狀態(tài) -->???
??<item??????
???android:state_pressed="true"???
???android:drawable="@drawable/thumb_normal"?????
???/>?????
??<!-- 普通無焦點(diǎn)狀態(tài) -->???
??<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 ;? //顯示當(dāng)前音量
? 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);? //音量設(shè)置
??????? mVolume = (TextView)findViewById(R.id.mVolume);?
??????? audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);?


??????? maxVolume = audiomanage.getStreamMaxVolume(AudioManager.STREAM_MUSIC);? //獲取系統(tǒng)最大音量
??????? soundBar.setMax(maxVolume);?? //拖動(dòng)條最高值與系統(tǒng)最大聲匹配
??????? currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM_MUSIC);? //獲取當(dāng)前值
??????? soundBar.setProgress(currentVolume);?
??????? mVolume.setText(currentVolume*100/maxVolume + " %");?
?
??????? soundBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){調(diào)音監(jiān)聽器
???????  ?public void onProgressChanged(SeekBar arg0,int progress,boolean fromUser){
????????????  ??audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);??
???????????  ???currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM_MUSIC);? //獲取當(dāng)前值
????????????  ? 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????

??    ?}
????? });

?

轉(zhuǎn)載于:https://www.cnblogs.com/zhangping/p/3514146.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的Android 使用SeekBar调节系统音量的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。