您的位置:首页 > 移动开发 > Android开发

自定义音乐播放进度条

2010-04-13 15:49 309 查看
[前提]

* android 自身也提供了该接口 似乎是:MediaController   发现极丑

[要求]

1. 进度条控件打算使用系统提供的SeekBar

2. SeekBar 要支持拖拉功能 即:定点播放

3. SeekBar 要反映播放位置 即:播放到哪 SeekBar 就在哪

[原理]

1. 音乐定点播放:MediaPlayer.seekTo(int msecond) //单位:毫秒

2. 音乐文件播放时间:MediaPlayer.getDuration()

3. SeekBar 获取位置:SeekBar.getProgress()

4. SeekBar 最大值: SeekBar.getMax()

[代码 步骤]

定义界面:main.xml

* Button 

: 

播放控制 

如:



停 

继续

  

* TextView 

: 



示播放百分比  

* SeekBar 

: 



度条  

* RadioGroup 

: 



示所有sdcard 





文件 

 

1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android" mce_href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"
3.     android:orientation="vertical"
4.     android:layout_width="fill_parent"
5.     android:layout_height="fill_parent"
6.     >
7. <LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android" mce_href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"
8.     android:orientation="horizontal"
9.     android:layout_width="fill_parent"
10.     android:layout_height="wrap_content"
11.     >
12. <Button
13.  android:id="@+id/cmd"
14.  android:text="Loading..."
15.  android:layout_width="90dip"
16.     android:layout_height="wrap_content"
17.     android:singleLine="true"
18. />
19. <TextView
20.  android:id="@+id/progress"
21.  android:text="Progress.."
22.  android:layout_width="50dip"
23.     android:layout_height="fill_parent"
24.     android:gravity="center"
25.     android:singleLine="true"
26. />
27. </LinearLayout>
28. <SeekBar
29.  android:id="@+id/seekb"
30.  android:max="100"
31.     android:layout_width="fill_parent"
32.     android:layout_height="wrap_content"
33.     />
34. </LinearLayout>


 

View初始化

 

1. public void initialize(){
2.
3.         sBar = (SeekBar)findViewById(R.id.seekb);
4.         rGroup = (RadioGroup)findViewById(R.id.radio);
5.         cmdButton = (Button)findViewById(R.id.cmd);
6.
7.         mPlayer = new MediaPlayer();
8.     }


 

拖动SeekBar,且播放指定位置的音乐

 

1. sBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
2.
3.             @Override
4.             public void onProgressChanged(SeekBar seekBar, int progress,
5.                     boolean fromUser) {
6.                 // TODO Auto-generated method stub
7.
8.             }
9.
10.             @Override
11.             public void onStartTrackingTouch(SeekBar seekBar) {
12.                 // TODO Auto-generated method stub
13.             }
14.
15.             @Override
16.             public void onStopTrackingTouch(SeekBar seekBar) {
17.                 // TODO Auto-generated method stub
18.                 int dest = seekBar.getProgress();
19.
20.                 int mMax = mPlayer.getDuration();
21.                 int sMax = sBar.getMax();
22.
23.                 mPlayer.seekTo(mMax*dest/sMax);
24.
25.             }
26.
27.         });


 

刷新播放位置,且使其实时变化

//mediaplayer没有播放进度的回调函数,所以只能开辟一个Thread定时使其刷新

 

1. public void startProgressUpdate(){
2.         //开辟Thread 用于定期刷新SeekBar
3.         DelayThread dThread = new DelayThread(100);
4.         dThread.start();
5.     }


 

Thread具体实现代码:

 

1. private Handler mHandle = new Handler(){
2.         @Override
3.         public void handleMessage(Message msg){
4.             int position = mPlayer.getCurrentPosition();
5.
6.             int mMax = mPlayer.getDuration();
7.             int sMax = sBar.getMax();
8.
9.             sBar.setProgress(position*sMax/mMax);
10.         }
11.     };
12.
13.     public class DelayThread extends Thread {
14.         int milliseconds;
15.
16.         public DelayThread(int i){
17.             milliseconds = i;
18.         }
19.         public void run() {
20.             while(true){
21.                 try {
22.                     sleep(milliseconds);
23.                 } catch (InterruptedException e) {
24.                     // TODO Auto-generated catch block
25.                     e.printStackTrace();
26.                 }
27.
28.                 mHandle.sendEmptyMessage(0);
29.             }
30.         }
31.     }


 

样图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息