安卓开发实现简易音乐播放器
文章目录
一、布局设置
设计简易音乐播放器的界面
1.主页面设计
代码如下:
<?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="match_parent"
android:background="#FFFFFF"
android:weightSum="3.5">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="4">
<ImageButton
android:id="@+id/previous"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:background="#FFFFFF"
android:src="@drawable/previous"/>
<ImageButton
android:id="@+id/play"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:background="#FFFFFF"
android:src="@drawable/play" />
<ImageButton
android:id="@+id/stop"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:background="#FFFFFF"
android:src="@drawable/stop" />
<ImageButton
android:id="@+id/next"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:background="#FFFFFF"
android:src="@drawable/next" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2.5"
android:gravity="center"
android:orientation="vertical"
android:background="#f4f4f4">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_margin="20dp"
android:text="歌曲名称"
android:textColor="#000000 "
android:textSize="36sp" />
<TextView
android:id="@+id/author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="#040404"
android:textSize="18sp"
android:text="歌手"/>
</LinearLayout>
</LinearLayout>
二、MainActivity实现功能
1.动作定义实现监听
代码如下:
static final String CTL_ACTION = "org.zzw.action.CTL_ACTION";
static final String UPDATE_ACTION = "org.zzw.action.UPDATE_ACTION";
// 定义音乐的播放状态,0x11代表没有播放;0x12代表正在播放;0x13代表暂停
int status = 0x11;
2.定义广播用于在service与activity中切换以及传输信号
代码如下(示例):
public class ActivityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int update = intent.getIntExtra("update",-1);
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;
}
}
}
3.实现图片四个按钮的监听
代码如下(示例):
public void onClick(View source)
{
// 创建Intent
Intent intent = new Intent(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.previous:
intent.putExtra("control", 3);
break;
// 按下下一首
case R.id.next:
intent.putExtra("control", 4);
break;
}
// 发送广播,将被Service组件中的BroadcastReceiver接收到
sendBroadcast(intent);
}
}
三、MusicService服务
1.音乐准备播放
代码如下(示例):
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();
}
}
2.循环绑定
实现音乐循环播放(此处设置为3首歌循环播放):
mPlayer = new MediaPlayer();
// 为MediaPlayer播放完成事件绑定监听器
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Log.d("MusicService", "歌曲播放完毕");
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]);
}
});
}
3.逻辑控制
通过status的数值表示当前的状态,0x11代表没有播放;0x12代表正在播放;0x13代表暂停:
public class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(final Context context, Intent intent)
{
// 获取Intent中的Control状态
int control = intent.getIntExtra("control", -1);
Log.d("MyReceiver", "MusicService has received a broadcast and the control is "+control);
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);
}
}