
//点击播放、暂停按钮
public void play(View view) {
Intent intent = new Intent();
intent.putExtra("ctrl", 1);
intent.setAction("org.itair.service.receiver");
sendBroadcast(intent);
}
//点击停止按钮
public void stop(View view) {
Intent intent = new Intent();
intent.putExtra("ctrl", 3);
intent.setAction("org.itair.service.receiver");
sendBroadcast(intent);
}
------------------------------------------------------------------------
Service中动态注册一个广播接收者:
ServiceRecevier serviceRecevier = new ServiceRecevier();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("org.itair.service.receiver");
registerReceiver(serviceRecevier, intentFilter);
Service中的BroadcastReceiver,收到后,同时也向Activity发送广播,(同样在Activity中也需要注册广播接收者)
public class ServiceRecevier extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 接收到Activity播放消息类型后,进行判断,调用服务中播放,暂停,停止的方法
int ctrl = intent.getExtras().getInt("ctrl", -1);
int type = -1;
switch (ctrl) {
case 1:
type = 1;
if (!isPlay) {
prepareAndStart(0, current);
} else {
pause();
}
break;
case 2:
pause();
break;
case 3:
stop();
type = 0;
break;
}
//接收消息后,同时向Activit发送在播放状态,播放曲目id给Activity
//Activity收到消息后,会播放按钮更新为暂停状态
Intent sendIntent = new Intent();
sendIntent.putExtra("type", type);
sendIntent.putExtra("status", isPlay);
sendIntent.setAction("org.itair.ui.receiver");
sendBroadcast(sendIntent);
}
}
Service中音乐播放时把播放进度用广播发送给Activity:
//Service中播放音乐的方法
public void prepareAndStart(int index, int current) {
try {
AssetFileDescriptor afd = am.openFd(musics[index]);
mPlayer.reset();
mPlayer.setDataSource(afd.getFileDescriptor(),
afd.getStartOffset(), afd.getLength());
mPlayer.prepare();
mPlayer.seekTo(current);
mPlayer.start();
isPlay = true;
final int total = mPlayer.getDuration();
Runnable runnable = new Runnable() {
Intent sendIntent = new Intent();
@Override
public void run() {
// TODO Auto-generated method stub
while (isPlay) {
// 发送进度广播给Activity
sendIntent.putExtra("type", 2);
sendIntent.putExtra("current",
100 * mPlayer.getCurrentPosition() / total);
sendIntent.setAction("org.itair.ui.receiver");
sendBroadcast(sendIntent);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
new Thread(runnable).start();
mPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
//播放完毕发送消息给Activity
isPlay = false;
Intent sendIntent = new Intent();
sendIntent.putExtra("type", 0);
sendIntent.putExtra("status", isPlay);
sendIntent.setAction("org.itair.ui.receiver");
sendBroadcast(sendIntent);
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
----------------------------------------------------------------------
相应的,Activity中的广播接收者,接收广播进行处理:
public class UiRecevier extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int type = intent.getIntExtra("type", -1);
switch (type) {
//更新播放按钮
case 0:
pb.setProgress(0);
btn_start.setImageResource(R.drawable.play);
break;
case 1:
boolean isPlay = intent.getExtras().getBoolean("status");
tv_singer.setText(titles[0]);
if (isPlay) {
btn_start.setImageResource(R.drawable.pause);
} else {
btn_start.setImageResource(R.drawable.play);
}
break;
case 2:
//更新进度
pb.setProgress(intent.getExtras().getInt("current"));
break;
}
}
}
二、采用绑定服务开启服务的方法暴露Service中的方法,Activity进行调用,Service利用Handler向Activity发送进度消息。(绑定服务+Handler进行通讯)
Activity中绑定服务:
conn = new MyConn();
Intent intent = new Intent(this, MusicService.class);
bindService(intent, conn, BIND_AUTO_CREATE);
Service中向Activity中发送播放进度消息(Handler+Message)
Runnable runnable = new Runnable() {
Intent sendIntent = new Intent();
Message message = null;
@Override
public void run() {
// TODO Auto-generated method stub
while (isPlay) {
//Handler message实现向Activity发送播放进度消息
message = new Message();
message.arg1 = 100 * mPlayer.getCurrentPosition()
/ total;
MainActivity.handler.sendMessage(message);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
new Thread(runnable).start();
----------------------------------------------------------------------
注意这个handler是在Activity中声明定义的,且要声明为public static。
Activity中,handler接收到消息后进行进度条的更新。
public static Handler handler;
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
pb.setProgress(msg.arg1);
if(msg.arg2==100){
pb.setProgress(0);
btn_start.setImageResource(R.drawable.play);
isPlay = false;
}
super.handleMessage(msg);
}
};