Android应用开发--MP3音乐播放器Service实现(1),android面试基础题记不住

//服务要发送的一些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.sendEmptyMessageDelayed(1, 1000);

}

}

};

};

@Override

public void onCreate() {

super.onCreate();

Log.d(“service”, “service created”);

mediaPlayer = new MediaPlayer();

mp3Infos = MediaUtil.getMp3Infos(PlayerService.this);

/**

  • 设置音乐播放完成时的监听器

*/

mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

@Override

public void onCompletion(MediaPlayer mp) {

if (status == 1) { // 单曲循环

mediaPlayer.start();

} else if (status == 2) { // 全部循环

current++;

if(current > mp3Infos.size() - 1) { //变为第一首的位置继续播放

current = 0;

}

Intent sendIntent = new Intent(UPDATE_ACTION);

sendIntent.putExtra(“current”, current);

// 发送广播,将被Activity组件中的BroadcastReceiver接收到

sendBroadcast(sendIntent);

path = mp3Infos.get(current).getUrl();

play(0);

} else if (status == 3) { // 顺序播放

current++; //下一首位置

if (current <=

  • 24
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值