public class MainActivity extends Activity {
static Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
Bundle bundle = msg.getData();
int duration = bundle.getInt("duration");
int currentPostition = bundle.getInt("currentPosition");
//刷新进度条的进度
sb.setMax(duration);
sb.setProgress(currentPostition);
}
};
MusicInterface mi;
private MyserviceConn conn;
private Intent intent;
private static SeekBar sb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sb = (SeekBar) findViewById(R.id.sb);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//根据拖动的进度改变音乐播放进度
int progress = seekBar.getProgress();
//改变播放进度
mi.seekTo(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
});
intent = new Intent(this, MusicService.class);
startService(intent);
conn = new MyserviceConn();
bindService(intent, conn, BIND_AUTO_CREATE);
}
class MyserviceConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mi = (MusicInterface) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
}
public void play(View v){
mi.play();
}
public void continuePlay(View v){
mi.continuePlay();
}
public void pause(View v){
mi.pause();
}
public void exit(View v){
unbindService(conn);
stopService(intent);
}
}
public class MusicService extends Service {
MediaPlayer player;
private Timer timer;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new MusicController();
}
class MusicController extends Binder implements MusicInterface{
@Override
public void play() {
MusicService.this.play();
}
@Override
public void pause() {
MusicService.this.pause();
}
@Override
public void continuePlay() {
MusicService.this.continuePlay();
}
@Override
public void seekTo(int progress) {
MusicService.this.seekTo(progress);
}
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
player = new MediaPlayer();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
//停止播放
player.stop();
//释放占用的资源,此时player对象已经废掉了
player.release();
player = null;
if(timer != null){
timer.cancel();
timer = null;
}
}
//播放音乐
public void play(){
//重置
player.reset();
try {
//加载多媒体文件
player.setDataSource("sdcard/zxmzf.mp3");
// player.setDataSource("http://192.168.13.119:8080/bzj.mp3");
// player.prepare();
player.prepareAsync();
// player.start();
player.setOnPreparedListener(new OnPreparedListener() {
//准备完毕时,此方法调用
@Override
public void onPrepared(MediaPlayer mp) {
player.start();
addTimer();
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//继续播放
public void continuePlay(){
player.start();
}
//暂停播放
public void pause(){
player.pause();
}
public void seekTo(int progress){
player.seekTo(progress);
}
public void addTimer(){
if(timer == null){
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
//获取歌曲总时长
int duration = player.getDuration();
//获取歌曲当前播放进度
int currentPosition= player.getCurrentPosition();
Message msg = MainActivity.handler.obtainMessage();
//把进度封装至消息对象中
Bundle bundle = new Bundle();
bundle.putInt("duration", duration);
bundle.putInt("currentPosition", currentPosition);
msg.setData(bundle);
MainActivity.handler.sendMessage(msg);
}
//开始计时任务后的5毫秒,第一次执行run方法,以后每500毫秒执行一次
}, 5, 500);
}
}
}