昨天Android MediaPlayer 一个简单的音乐播放器实例,我们学习了MediaPlayer的初级用法,今天,我们在昨天的基础上,给播放器加个SeekBar,实现显示播放进度,以及快进快退。
先在main.xml里加上个SeekBar,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="播放"
/>
<Button android:id="@+id/pause"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="暂停"
/>
<SeekBar android:id="@+id/sb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
/>
</LinearLayout> |
程序代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
| package com.pocketdigi;
import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class main extends Activity {
/** Called when the activity is first created. */
Button play,pause;
MediaPlayer mp;
SeekBar sb;
Handler handler=new Handler();
int Duration;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play=(Button)findViewById(R.id.play);
pause=(Button)findViewById(R.id.pause);
sb=(SeekBar)findViewById(R.id.sb);
//找到相应View
mp =MediaPlayer.create(this,Uri.parse("/sdcard/徐若瑄-爱笑的眼睛.mp3"));
//后面的参数必须是URI形式的,所以要把相应路径转换成URI
play.setOnClickListener(playlis);
pause.setOnClickListener(pauselis);
sb.setOnSeekBarChangeListener(sbLis);
//监听器
Duration=mp.getDuration();
//音乐文件持续时间
sb.setMax(Duration);
//设置SeekBar最大值为音乐文件持续时间
}
private OnClickListener playlis=new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
handler.post(start);
//调用handler播放
}
};
Runnable start=new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
mp.start();
handler.post(updatesb);
//用一个handler更新SeekBar
}
};
Runnable updatesb =new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
sb.setProgress(mp.getCurrentPosition());
handler.postDelayed(updatesb, 1000);
//每秒钟更新一次
}
};
private OnClickListener pauselis=new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp.pause();
//暂停
}
};
private OnSeekBarChangeListener sbLis=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
mp.seekTo(sb.getProgress());
//SeekBar确定位置后,跳到指定位置
}
};
} |