Android开发-使用广播设计简易音乐盒
一、功能说明
大家在使用手机音乐播放器的时候,都会看到“上一首”、“暂停 / 播放”、“重新开始”、“下一首”这几个按钮。
可以看到,屏幕最上方显示歌曲名与歌手名,下方摆放四个按钮,分别进行“上一首”、“暂停 / 播放”、“重新开始”、“下一首”的操作。本文就是讲述如何实现这几个按钮的功能,播放准备好的音乐资源文件。
二、程序分析
在本案例中,在广播内内嵌服务,设计简易的音乐盒。大致分为 UI 设计与功能设计两部分。
(一)UI设计
-
首先将所有的图片资源放入【res】-【drawable】文件夹中,由于 播放 / 暂停 按钮要做出点击与未点击的不同效果,所以一共有五张图片。
-
顺便要将音乐资源放入【res】-【raw】文件夹中,由于raw 文件夹一开始是没有的,所以需要自己新建:
-
在layout文件夹中创建main.xml文件,并设置其样式。
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="180dp">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_weight="1"
android:gravity="center"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:textColor="#9C27B0"
android:textSize="25sp"
android:text="歌曲名"/>
<TextView
android:id="@+id/author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="歌手名"
android:textSize="25sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="90dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/pre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pre" />
<ImageButton
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/play"/>
<ImageButton
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/stop"/>
<ImageButton
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/next" />
</LinearLayout>
</LinearLayout>
(二)功能设计
MainActivity.java
- 在
MainActivity.java
中,在最外层定义所需要用到的变量和子类。 - onCreate()函数用于获取相应按钮以及添加监听器、注册receiver。
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取程序界面界面中的两个按钮
front=(ImageButton)this.findViewById(R.id.front);
play = (ImageButton) this.findViewById(R.id.play);
stop = (ImageButton) this.findViewById(R.id.stop);
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);
front.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);
}
- ActivityReceiver子类,负责监听从Service传回来的广播,并用switch控制系统状态,且定义了在音乐播放的不同状态下显示的图片。
// 自定义的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.stop);
// 设置当前状态
status = 0x12;
break;
// 控制系统进入暂停状态
case 0x13:
// 暂停状态下设置使用播放图标
play.setImageResource(R.drawable.play);
// 设置当前状态
status = 0x13;
break;
}
}
}
- onClick函数,用switch判断点击事件,向Service发送广播。
@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.front:
intent.putExtra("control",3);
break;
//按下下一首
case R.id.next:
intent.putExtra("control",4);
break;
}
// 发送广播,将被Service组件中的BroadcastReceiver接收到
sendBroadcast(intent);
}
MusicService.java
- 在
MusicService.java
中,在最外层定义所需要用到的变量和子类。 - onCreate函数添加监听器、注册receiver,并向Activity发送广播。
@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 MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
current++;
if (current>=4)
{
current=0;
}
//发送广播通知Activity更改文本框
Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
sendIntent.putExtra("current",current);
//发送广播,将被Activity组件中的BroadcastReceiver接收
sendBroadcast(sendIntent);
//准备播放音乐
prepareAndPlay(musics[current]);
}
});
}
- MyReceiver子类,接收MainActivity发来的信息,并发送广播,定义了接收到来自MainActivity的广播后的处理过程。
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();
}
}
三、运行界面
无论当前是在播放还是暂停状态,当点击“下一首”按钮时,可以进入下一首歌曲,歌曲名与歌手名会切换。
四、完整源代码
源码gitee地址:使用广播设计简易音乐盒源代码