[要求]
1. 进度条
控件打算使用
系统提供的SeekBar
2. SeekBar 要支持拖拉
功能 即:定点
播放3. SeekBar 要反映播放位置 即:播放到哪 SeekBar 就在哪
[原理]
1. 音乐定点播放:MediaPlayer.seekTo(int msecond) //单位:毫秒
2. 音乐
文件播放
时间:MediaPlayer.getDuration()
3. SeekBar
获取位置:SeekBar.getProgress()
4. SeekBar 最大值: SeekBar.getMax()
[组件]
1、Button : 播放控制 如:暂停 继续
2、
TextView : 显示播放百分比
3、SeekBar : 进度条
4、RadioGroup : 显示所有sdcard 音乐文件
[
代码 步骤]
1.
定义界面:main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <Button
- android:id="@+id/cmd"
- android:text="Loading..."
- android:layout_width="90dip"
- android:layout_height="wrap_content"
- android:singleLine="true"
- />
- <TextView
- android:id="@+id/progress"
- android:text="Progress.."
- android:layout_width="50dip"
- android:layout_height="fill_parent"
- android:gravity="center"
- android:singleLine="true"
- />
- </LinearLayout>
- <SeekBar
- android:id="@+id/seekb"
- android:max="100"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
复制代码
2. View 初始化
- public void initialize(){
- sBar = (SeekBar)findViewById(R.id.seekb);
- rGroup = (RadioGroup)findViewById(R.id.radio);
- cmdButton = (Button)findViewById(R.id.cmd);
- mPlayer = new MediaPlayer();
- }
复制代码
3. 拖动SeekBar 且播放指定位置的音乐
- sBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress,
- boolean fromUser) {
- // TODO Auto-generated method stub
-
- }
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- // TODO Auto-generated method stub
- int dest = seekBar.getProgress();
-
- int mMax = mPlayer.getDuration();
- int sMax = sBar.getMax();
-
- mPlayer.seekTo(mMax*dest/sMax);
-
- }
-
- });
复制代码
4. 刷新播放位置 且使其实时变化
//因为MediaPlayer没有播放进度的回调函数 所以只能:开辟一个Thread 定时通知其刷新
- public void startProgressUpdate(){
- //开辟Thread 用于定期刷新SeekBar
- DelayThread dThread = new DelayThread(100);
- dThread.start();
- }
复制代码
而该Thread 具体实现为:
- private Handler mHandle = new Handler(){
- @Override
- public void handleMessage(Message msg){
- int position = mPlayer.getCurrentPosition();
-
- int mMax = mPlayer.getDuration();
- int sMax = sBar.getMax();
-
- sBar.setProgress(position*sMax/mMax);
- }
- };
- public class DelayThread extends Thread {
- int milliseconds;
-
- public DelayThread(int i){
- milliseconds = i;
- }
- public void run() {
- while(true){
- try {
- sleep(milliseconds);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- mHandle.sendEmptyMessage(0);
- }
- }
- }
复制代码
5. emulator 运行截图: