项目要求
- 有播放、暂停、停止功能
- 有上一首和下一首功能
- 显示歌名及歌手
项目效果展示
核心代码
主页面布局文件
MainActivity
- 定义的变量
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[] {"MOM","夏天的风","微微","I don't wanna see u anymore"};
String[] authorStrs = new String[] {"蜡笔小新","温岚","傅如乔","NINEONE"};
- onCreate()函数
@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);
}
- onClick()函数
@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);
}
- ActivityReceiver()函数
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||update==0x14))
{
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;
case 0x14:
break;
}
}
}
MusicService
- 定义的变量
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;
- onCreate()函数
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("update",0x14);
sendIntent.putExtra("current",current);
//发送广播,将被Activity组件中的BroadcastReceiver接收
sendBroadcast(sendIntent);
//准备播放音乐
prepareAndPlay(musics[current]);
}
});
}
- MyReceiver类里的onReceive()函数
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);
}
}
- prepareAndPlay()函数
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();
}
}
项目总结
本次项目实现了音乐的播放、暂停、切换等功能,Service与之前学的Activity有相似之处,经过对比巩固加深了之前的知识。广播组件是新的一种信息传递的方式,对之前的sendIntent等方式是有效的补充。通过制作音乐播放器,对安卓开发有了更深的理解。