*/
@SuppressLint(“NewApi”)
public class PlayerService extends Service {
private MediaPlayer mediaPlayer; // 媒体播放器对象
private String path; // 音乐文件路径
private int msg;
private boolean isPause; // 暂停状态
private int current = 0; // 记录当前正在播放的音乐
private List mp3Infos; //存放Mp3Info对象的集合
private int status = 3; //播放状态,默认为顺序播放
private MyReceiver myReceiver; //自定义广播接收器
private int currentTime; //当前播放进度
private int duration; //播放长度
//服务要发送的一些Action
public static final String UPDATE_ACTION = “com.wwj.action.UPDATE_ACTION”; //更新动作
public static final String CTL_ACTION = “com.wwj.action.CTL_ACTION”; //控制动作
public static final String MUSIC_CURRENT = “com.wwj.action.MUSIC_CURRENT”; //当前音乐播放时间更新动作
public static final String MUSIC_DURATION = “com.wwj.action.MUSIC_DURATION”;//新音乐长度更新动作
/**
- handler用来接收消息,来发送广播更新播放时间
*/
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 1) {
if(mediaPlayer != null) {
currentTime = mediaPlayer.getCurrentPosition(); // 获取当前音乐播放的位置
Intent intent = new Intent();
intent.setAction(MUSIC_CURRENT);
intent.putExtra(“currentTime”, currentTime);
sendBroadcast(intent); // 给PlayerActivity发送广播
handler.sendEmptyMessageDelay