Andriod Studio编写简单的音乐播放器
实验内容
运用广播和服务的知识编写音乐盒代码,参考群文件-源码-Musicbox,掌握源码后在其基础上添加音乐播放的 上一首 和 下一首 控制,也可以增加更多的功能。
实验效果展示
界面布局


点击播放按钮


项目代码
项目代码目录

界面布局文件
main.xml
包含了三个LinearLayout,其中一个是整体的,还有两个内嵌的,内嵌的有一个是包含的四个ImageButton,用来展示播放、暂停、上一首、下一首等控件;另一个是包含的两个TextView,用来显示歌曲名和歌手名。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFFFFF"
android:weightSum="3.5">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="4">
<ImageButton
android:id="@+id/play"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#FFFFFF"
android:src="@drawable/play"/>
<ImageButton
android:id="@+id/lastone"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#FFFFFF"
android:src="@drawable/last" />
<ImageButton
android:id="@+id/nextone"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#FFFFFF"
android:src="@drawable/next" />
<ImageButton
android:id="@+id/finish"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#FFFFFF"
android:src="@drawable/finish" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2.5"
android:gravity="center">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:textColor="#9C27B0"
android:text="歌曲名称"
android:textSize="50dp"
android:ellipsize="marquee"
android:gravity="center_horizontal"
android:marqueeRepeatLimit="marquee_forever"/>
<TextView
android:id="@+id/author"
android:textSize="40dp"
android:gravity="center_vertical"
android:text="歌手"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
MusicService服务类代码
定义变量
MyReceiver serviceReceiver;
AssetManager am;
String[] musics = new String[] { "legendsneverdie.mp3", "promise.mp3","whis.mp3",
"beautiful.mp3" };
MediaPlayer mPlayer;
// 当前的状态,0x11代表没有播放;0x12代表正在播放;0x13代表暂停
int status = 0x11;
// 记录当前正在播放的音乐
int current = 0;
准备音乐
打开指定音乐文件,使用MediaPlayer加载指定的声音文件,获取音乐文件的名字等信息,准备声音然后播放
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();
}
}
}
循环绑定
为音乐循环播放绑定监听器使得播放器在不操作情况下能循环播放,在播放完最后一首歌曲后会自动跳转到第一首歌
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 >= 4)
{
current = 0;
}
//发送广播通知Activity更改文本框
Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
sendIntent.putExtra("current", current);
// 发送广播,将被Activity组件中的BroadcastReceiver接收到
sendBroadcast(sendIntent);
// 准备并播放音乐
prepareAndPlay(musics[current]);
}
});
}
点击每个按钮的各种情况
当点击每个按钮的时候,会出现各种情况,有四个按钮,所以有四种case,比如正在暂停时点击播放按钮,在播放音乐时点击暂停按钮……广播通知Activity更改图标、文本框。
(其中status的数值表示当前的状态,0x11代表没有播放;0x12代表正在播放;0x13代表暂停)
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;
}
break;
//上一首
case 3:
//如果原来正在播放或者暂停
if(status == 0x12 || status == 0x13) {
mPlayer.stop();
if(current - 1 < 0){
current = musics.length - 1;
}else {
current--;
}
prepareAndPlay(musics[current]);
status = 0x12;
}
break;
//下一首
case 4:
if(status == 0x12 || status == 0x13) {
mPlayer.stop();
if(current + 1 >= musics.length){
current = 0;
}else {
current++;
}
prepareAndPlay(musics[current]);
status = 0x12;
}
break;
}
// 广播通知Activity更改图标、文本框
Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
sendIntent.putExtra("update", status);
sendIntent.putExtra("current", current);
// 发送广播,将被Activity组件中的BroadcastReceiver接收到
sendBroadcast(sendIntent);
}
}
MainActivity主代码
定义动作来进行监听
定义两个动作分别用于MainActivity和MusicService 的监听
public static final String CTL_ACTION = "org.lh.action.CTL_ACTION";
public static final String UPDATE_ACTION = "org.lh.action.UPDATE_ACTION";
定义变量
定义音乐的播放状态,0x11代表没有播放;0x12代表正在播放;0x13代表暂停
int status = 0x11;
// 获取界面中显示歌曲标题、作者文本框
TextView title, author;
// 播放/暂停、停止按钮
ImageButton play, stop, last, next;
ActivityReceiver activityReceiver;
根据id来绑定界面的中的各个控件
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取程序界面界面中的两个按钮
play = (ImageButton) this.findViewById(R.id.play);
stop = (ImageButton) this.findViewById(R.id.finish);
last = findViewById(R.id.lastone);
next = findViewById(R.id.nextone);
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);
}
自定义广播
用于在service与activity中切换以及传输信号,自定义的BroadcastReceiver,负责监听从Service传回来的广播
// 自定义的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;
}
}
}
对四个ImageButton进行动作监听
@Override
public void onClick(View source)
{
// 创建Intent
Intent intent = new Intent("org.lh.action.CTL_ACTION");
switch (source.getId())
{
// 按下播放/暂停按钮
case R.id.play:
intent.putExtra("control", 1);
break;
// 按下停止按钮
case R.id.finish:
intent.putExtra("control", 2);
break;
case R.id.lastone:
intent.putExtra("control",3);
break;
case R.id.nextone:
intent.putExtra("control",4);
break;
}
// 发送广播,将被Service组件中的BroadcastReceiver接收到
sendBroadcast(intent);
}

本教程介绍如何使用Android Studio创建一个具备基本功能的音乐播放器应用,包括播放、暂停、上一首及下一首等功能,并实现音乐循环播放。通过广播和服务机制交互,确保应用程序能够在后台继续播放音乐。

8万+

被折叠的 条评论
为什么被折叠?



