基本信息
源码名称:Android 音乐播放器
源码大小:14.70M
文件格式:.zip
开发语言:Java
更新时间:2021-06-13
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
Android studio 使用Service及BroadcastReceiver实现音乐播放器
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); } }