项目要求
- 使用broadcast和service实现音乐播放、暂停、切换
- 播放时能显示歌曲名与歌手名
演示效果
项目结构
实现思路
- MainActivity启动服务,将播放、暂停、停止、切换信号发送广播给Service
- MusicService 调用函数Prepareandplay()播放音乐,发送广播给MainActivity返回歌曲id
- PrepareAndPlay() 定向操作 打开并播放音乐文件
记录
- assets相比res管理更松散,可以定义下级子文件夹,且assets中的资源不占用APK空间
- 原来是BroadcastReceiver,现在在Activity里,创建一个ActivityReceiver子类来代替其功能
- BroadcastReceiver可以接收来自系统和应用的的广播,但来自系统的广播不用我们人为发送
主要代码
MainActivity.java
public class MainActivity extends Activity implements View.OnClickListener {
TextView title, author;
ImageButton play, stop,last,next;
ActivityReceiver activityReceiver;
public static final String CTL_ACTION=
"org.crazyit.action.CTL_ACTION";
public static final String UPDATE_ACTION=
"org.crazyit.action.UPDATE_ACTION";
int status = 0x11;
String[] titleStrs = new String[] {"Take Me Home Country Roads","好想爱这个世界啊","You Are Not Alone","七里香"};
String[] authorStrs = new String[] {"John Denver","华晨宇","Michael Jackson","周杰伦"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activityReceiver = new ActivityReceiver();
IntentFilter filter = new IntentFilter();
//指定BroadCastReceiver监听的action
filter.addAction(UPDATE_ACTION);
registerReceiver(activityReceiver,filter);
Intent intent = new Intent(this,MusicService.class);
startService(intent);
//找到对应控件
play = this.findViewById(R.id.imgplay);
stop = this.findViewById(R.id.imgstop);
next = this.findViewById(R.id.imgnext);
last = this.findViewById(R.id.imglast);
title = this.findViewById(R.id.txttitle);
author = this.findViewById(R.id.txtauthor);
//添加监听
play.setOnClickListener(this);
stop.setOnClickListener(this);
next.setOnClickListener(this);
last.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent intent = new Intent("org.crazyit.action.CTL_ACTION");
//在主界面按下对应按钮,传递给service对应参数
switch (view.getId())
{
case R.id.imgplay:
intent.putExtra("control",1);
break;
case R.id.imgstop:
intent.putExtra("control",2);
break;
case R.id.imgnext:
intent.putExtra("control",3);
break;
case R.id.imglast:
intent.putExtra("control",4);
break;
}
sendBroadcast(intent);
}
private class ActivityReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//获取来自receive中intent的update消息,代表播放状态
int update = intent.getIntExtra("update",-1);
//获取来自receive中intent的curruent消息,代表正在播放的歌曲
int current = intent.getIntExtra("current",-1);
//如果状态为正在播放歌曲或暂停
if(current>=0&&(update == 0x12||update == 0x13))
{
title.setText(titleStrs[current]);
author.setText(authorStrs[current]);
}
//如果状态为未播放歌曲
else
{
title.setText("未播放歌曲");
author.setText("未播放歌曲");
}
switch (update)
{
//如果未播放歌曲,则播放图标为播放
case 0x11:
play.setImageResource(R.drawable.play);
status=0x11;
break;
//如果正在播放歌曲,则播放图标为暂停
case 0x12:
play.setImageResource(R.drawable.pause);
status=0x12;
break;
case 0x13:
play.setImageResource(R.drawable.play);
status=0x13;
break;
}
}
}
}
MusicService.java
public class MusicService extends Service {
@Nullable
MyReceiver serviceReceiver;
AssetManager am;
String[] musics=new String[]{"music0.mp3","music1.mp3","music2.mp3","music3.mp3"};
MediaPlayer mPlayer;
//0x11表示没有播放,0x12代表正在播放,0x13代表暂停
int status=0x11;
int current=0;
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate(){
super.onCreate();
am=getAssets();
//创建BroadcastReceiver
serviceReceiver=new MyReceiver();
//创建IntentFilter
IntentFilter filter=new IntentFilter();
filter.addAction(MainActivity.CTL_ACTION);
registerReceiver(serviceReceiver,filter);
mPlayer=new MediaPlayer();
//为MediaPlayer播放完成事件绑定监听器
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
current++;
if (current>=4)
{
current=0;
}
Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
sendIntent.putExtra("current",current);
//发送广播,将被Activity组件中的BroadcastReceiver接收
sendBroadcast(sendIntent);
//准备播放音乐
prepareAndPlay(musics[current]);
}
});
}
public class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
int control =intent.getIntExtra("control",-1);
switch (control)
{
//播放或暂停
case 1:
//原来处于没有播放状态
if (status==0x11)
{
//准备并播放音乐
prepareAndPlay(musics[current]);
status=0x12;
}
//原来处于播放状态
else if (status==0x12)
{
//暂停
mPlayer.pause();
//改变为暂停状态
status=0x13;
}
//原来处于暂停状态
else if (status==0x13)
{
//播放
mPlayer.start();
//改变状态
status=0x12;
}
break;
//停止声音
case 2:
//如果原来正在播放或暂停
if (status==0x12||status==0x13) {
//停止播放
mPlayer.stop();
status = 0x11;
}
break;
case 3:
//原来处于没有播放或暂停状态
if (status==0x11||status==0x13)
{
if(current==0) {
current=3;
prepareAndPlay(musics[current]);
}
//准备并播放音乐
else {
current=current-1;
prepareAndPlay(musics[current]);
}
status=0x12;
}
//原来处于播放状态
else if (status==0x12)
{
//上一首//准备并播放音乐
if(current==0) {
current=3;
prepareAndPlay(musics[current]);
}
else {
current=current-1;
prepareAndPlay(musics[current]);
}
}
break;
case 4:
//原来处于没有播放或暂停状态
if (status==0x11||status==0x13)
{
if(current==3) {
current=0;
prepareAndPlay(musics[current]);
} //准备并播放音乐
else {
current=current+1;
prepareAndPlay(musics[current]);
}
status=0x12;
}
else if (status==0x12)
{
if(current==3) {
current=0;
prepareAndPlay(musics[current]);
}
else {
current=current+1;
prepareAndPlay(musics[current]);
}
}
break;
}
//广播通知Activity更改图标、文本框
Intent sendIntent=new Intent(MainActivity.UPDATE_ACTION);
sendIntent.putExtra("update",status);
sendIntent.putExtra("current",current);
//发送广播,将被Activity组件中的BroadcastReceiver接收
sendBroadcast(sendIntent);
}
}
private void prepareAndPlay(String music)
{
try
{
//打开指定音乐文件
AssetFileDescriptor afd=am.openFd(music);
mPlayer.reset();
//使用MediaPlayer加载指定的音乐文件
mPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
//准备声音
mPlayer.prepare();
//播放
mPlayer.start();
}catch (IOException e) {
e.printStackTrace();
}
}
}
全部代码
我的码云:MyMusicBox