移动开发技术作业2:运用广播和服务编写简单音乐盒
需求分析及功能介绍
本次技术作业要求是基于广播和服务技术编写一个简单的音乐盒,能够实现音乐的播放、暂停、上一首、下一首的功能。
采用Service组件实现后台播放,BroadcastReceiver实现消息传递。BroadcastReceiver是一种全局监听器,可以让不同组件之间进行通信。该音乐盒是一个基于Service组件的音乐盒,音乐由后台运行的service组件播放,当后台播放状态改变时,通过发送广播通知前台Activity更新界面;当用户单击前台Activity界面按钮,发送广播通知后台Service改变播放状态。
实验内容
1.资源的添加
图片资源复制到res/drawable文件夹下即可
音乐资源首先要在app下新建一个assets文件夹,然后将MP3文件存放到该文件夹下
2.页面UI布局设计
两个TextView控件显示歌曲的信息
四个ImageButtons控件实现播放、停止、上一首、下一首的功能
3.在AndroidManifest.xml里配置service
<service
android:name=".MusicService"
android:enabled="true"
android:exported="true"></service>
4.逻辑控制代码
MainActivity.java
在函数最外层定义所需要用到的变量和子类。其中activityReceiver用于监听Service传回的广播;onCreate()函数用于获取相应按钮以及添加监听器、注册receiver;ActivityReceiver子类,负责监听从Service传回来的广播,并用switch控制系统状态,且定义了在音乐播放的不同状态下显示的图片;onClick函数,用switch判断点击事件,向Service发送广播。
public class MainActivity extends Activity implements OnClickListener
{
// 获取界面中显示歌曲标题、作者文本框
TextView title, author;
// 播放、暂停、停止、上一曲和下一曲按钮
ImageButton play, stop;
ImageButton last,next;
ActivityReceiver activityReceiver;
public static final String CTL_ACTION =
"org.xr.action.CTL_ACTION";
public static final String UPDATE_ACTION =
"org.xr.action.UPDATE_ACTION";
// 定义音乐的播放状态,0x11代表没有播放;0x12代表正在播放;0x13代表暂停
int status = 0x11;
String[] titleStrs = new String[] { "Mojito","可惜没如果","无条件","月光" };
String[] authorStrs = new String[] { "周杰伦","林俊杰","陈奕迅","胡彦斌" };
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取程序界面界面中的播放,终止两个按钮
play = (ImageButton) this.findViewById(R.id.play);
stop = (ImageButton) this.findViewById(R.id.stop);
//获取程序界面中的上一曲,下一曲两个按钮
last=(ImageButton) this.findViewById(R.id.last);
next=(ImageButton) this.findViewById(R.id.next);
title = (TextView) findViewById(R.id.title);
author = (TextView) findViewById(R.id.author);
// 为播放,终止两个按钮的单击事件添加监听器
play.setOnClickListener(this);
stop.setOnClickListener(this);
// 为上/下一曲两个按钮的单击事件添加监听器
last.setOnClickListener(this);
next.setOnClickListener(this);
activityReceiver = new ActivityReceiver();
// 创建IntentFilter
IntentFilter filter = new IntentFilter();
// 指定BroadcastReceiver监听的Action
filter.addAction(UPDATE_ACTION);
// 注册BroadcastReceiver
registerReceiver(activityReceiver, filter);
Intent intent = new Intent(this, MusicService.class);
// 启动后台Service
startService(intent);
}
// 自定义的BroadcastReceiver,负责监听从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);
if (current >= 0)
{
title.setText(titleStrs[current]);
author.setText(authorStrs[current]);
}
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;
}
}
}
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View source)
{
// 创建Intent
Intent intent = new Intent("org.xr.action.CTL_ACTION");
switch (source.getId())
{
// 按下播放/暂停按钮
case R.id.play:
intent.putExtra("control", 1);
break;
// 按下停止按钮
case R.id.stop:
intent.putExtra("control", 2);
break;
//按下上一曲按钮
case R.id.last:
intent.putExtra("control", 3);
//按下下一曲按钮
case R.id.next:
intent.putExtra("control", 4);
}
// 发送广播,将被Service组件中的BroadcastReceiver接收到
sendBroadcast(intent);
}
}
MusicService.java
public class MusicService extends Service
{
MyReceiver serviceReceiver;
AssetManager am;
String[] musics = new String[] { "Mojito.mp3", "可惜没如果.mp3", "无条件.mp3","月光,mp3" };
MediaPlayer mPlayer;
// 当前的状态,0x11代表没有播放;0x12代表正在播放;0x13代表暂停
int status = 0x11;
// 记录当前正在播放的音乐
int current = 0;
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onCreate()
{
super.onCreate();
am = getAssets();
// 创建BroadcastReceiver
serviceReceiver = new MyReceiver();
// 创建IntentFilter
IntentFilter filter = new IntentFilter();
filter.addAction(MainActivity.CTL_ACTION);
registerReceiver(serviceReceiver, filter);
// 创建MediaPlayer
mPlayer = new MediaPlayer();
// 为MediaPlayer播放完成事件绑定监听器
mPlayer.setOnCompletionListener(new OnCompletionListener() // ①
{
@Override
public void onCompletion(MediaPlayer mp)
{
current++;
if (current >= 3)
{
current = 0;
}
//发送广播通知Activity更改文本框
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(final 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;
}
case 3:
current--;
if(current < 0)//已经到最前一曲,无法在前进
current = 0;
if (status == 0x12)
{
// 准备并播放音乐
prepareAndPlay(musics[current]);
status = 0x11;
}
//下一首
case 4:
current++;
if(current > 2)//已经到最后一曲,跳转至第一曲
current = 0;
if (status == 0x12)
{
// 准备并播放音乐
prepareAndPlay(musics[current]);
status = 0x11;
}
}
// 广播通知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();
}
}
}
实验结果展示
代码仓库
链接: 代码仓库