实例介绍
【实例简介】Android studio 使用Service及BroadcastReceiver实现音乐播放器
【实例截图】
【核心代码】
【实例截图】

【核心代码】
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ //定义音乐播放状态,0x10代表上一首,0x11代表没有播放或暂停,0x12代表正在播放,0x13代表下一首,0x15表示暂停 int status = 0x11; static long mediaPlayerDuration = 1L; static int sum=0; //三个按钮 Button prve,playorpause,next; static SeekBar seekBar; //歌曲名 static TextView songname,starttime,endtime; ActivityReceiver mActivityReceiver; String[] name=new String[]{"打击乐器","康加舞","蓝调小号","手拍鼓"}; public static final String CTL_ACTION = "com.trampcr.action.CTL_ACTION"; public static final String UPDATE_ACTION = "com.trampcr.action.UPDATE_ACTION"; //运用Handler中的handleMessage方法接收service传递的音乐播放进度信息 public static Handler handler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { // super.handleMessage(msg); // 将SeekBar位置设置到当前播放位置, // msg.arg1是service传过来的音乐播放进度信息,将其设置为进度条进度 seekBar.setProgress(msg.arg1/((int)(mediaPlayerDuration*10))); //将进度时间其转为mm:ss时间格式 starttime.setText(new SimpleDateFormat("mm:ss", Locale.getDefault()).format(new Date(msg.arg1))); return false; } }); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); songname=findViewById(R.id.songName); starttime=findViewById(R.id.MusicTime); endtime=findViewById(R.id.MusicTotal); prve=findViewById(R.id.BtnPrev); playorpause=findViewById(R.id.BtnPlayorStop); next=findViewById(R.id.BtnNext); seekBar=findViewById(R.id.MusicSeekBar); prve.setOnClickListener(this); playorpause.setOnClickListener(this); next.setOnClickListener(this); mActivityReceiver = new ActivityReceiver(); //创建IntentFilter IntentFilter filter = new IntentFilter(); //指定BroadcastReceiver监听的Action filter.addAction(UPDATE_ACTION); //注册BroadcastReceiver registerReceiver(mActivityReceiver, filter); Intent intent = new Intent(MainActivity.this, MusicService.class); //启动后台Service startService(intent); Log.i("1","启动服务"); } //接收service的消息 public class ActivityReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //获取Intent中的update消息,update代表播放状态 int update = intent.getIntExtra("update", -1); //获取Intent中的current消息,current代表当前正在播放的歌曲 int current = intent.getIntExtra("current", -1); int pro=intent.getIntExtra("process",-1); mediaPlayerDuration=intent.getLongExtra("endtime",-1); seekBar.setProgress(pro); String time=null; if(mediaPlayerDuration<10){ time="0" mediaPlayerDuration; } else { time="" mediaPlayerDuration; } songname.setText(name[current]); switch (update){ case 0x11: playorpause.setText("PLAY"); endtime.setText("00:" time); status = 0x11; break; //控制系统进入播放状态 case 0x12: //在播放状态下设置使用暂停图标 playorpause.setText("PAUSE"); endtime.setText("00:" time); status = 0x12; break; case 0x15: playorpause.setText("PLAY"); endtime.setText("00:" time); status = 0x15; break; } } } @Override public void onClick(View v) { Intent intent = new Intent(CTL_ACTION); Log.i("1","发送消息"); switch (v.getId()){ case R.id.BtnPrev: intent.putExtra("control", 1); break; case R.id.BtnPlayorStop: if(status==0x11||status==0x15){ intent.putExtra("control", 2); } else if(status==0x12){ intent.putExtra("control", 3); } break; case R.id.BtnNext: intent.putExtra("control", 4); break; } //发送广播,将被Service中的BroadcastReceiver接收到 sendBroadcast(intent); } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论