文章目录
一、项目地址
https://gitee.com/lonelyZhe/Android-musicbox
二、项目效果
三、Broadcast实现模式
1. 广播流程图
2. 为什么要使用广播实现?
因为现在的很多都是在线音乐盒,在我们点击播放一首歌曲的时候,要从服务器拿数据,然后播放,这其实不止一个流程,可以理解为多线程的,普通的顺序过程实现效果不好。所以我们专门做一个activity来控制音乐的播放,音乐盒界面只实现用户点击动作。
四、主要代码
1. activity_main.xml
这里实现音乐盒的布局
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/cover"
android:layout_width="match_parent"
android:layout_height="600dp"
android:layout_weight="1"
android:src="@drawable/wish" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_weight="1">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_weight="1"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:text="歌曲名"
android:gravity="center"
android:textColor="#000000"
android:textSize="30dp" />
<TextView
android:id="@+id/author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="艺术家"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="90dp"
android:gravity="center"
android:layout_marginBottom="10dp">
<ImageButton
android:id="@+id/pre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pre"
android:background="#FFFFFF"/>
<ImageButton
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/play"
android:background="#FFFFFF"/>
<ImageButton
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/next"
android:background="#FFFFFF"/>
<!--停止在这不太好看,将其宽度设置为0,先隐藏起来-->
<ImageButton
android:id="@+id/stop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@drawable/stop"
android:background="#FFFFFF"/>
</LinearLayout>
</LinearLayout>
2. MainActivaty.java
这里要实现发送广播和接收音乐服务返回的广播,实现用户点击操作
package com.lonelyzhe.musicbox;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener
{
// 获取界面中显示歌曲标题、作者文本框
TextView title, author;
// 播放/暂停、停止按钮
ImageButton play, stop;
// 上一首,下一首按钮
ImageButton pre, next;
// 获取封面
ImageView cover;
ActivityReceiver activityReceiver;
public static final String CTL_ACTION =
"org.crazyit.action.CTL_ACTION";
public static final String UPDATE_ACTION =
"org.crazyit.action.UPDATE_ACTION";
// 定义音乐的播放状态,0x11代表没有播放;0x12代表正在播放;0x13代表暂停
int status = 0x11;
String[] titleStrs = new String[] { "心愿", "约定", "美丽新世界" };
String[] authorStrs = new String[] { "四个女生", "周蕙", "伍佰" };
Integer[] covers = new Integer[] { R.drawable.wish, R.drawable.promise, R.drawable.beautiful};
@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);
title = (TextView) findViewById(R.id.title);
author = (TextView) findViewById(R.id.author);
cover = findViewById(R.id.cover);
pre = this.findViewById(R.id.pre);
next = this.findViewById(R.id.next);
// 为两个按钮的单击事件添加监听器
play.setOnClickListener(this);
stop.setOnClickListener(this);
// 为上一首、下一首的单击事件添加监听器
pre.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]);
cover.setImageResource(covers[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;
}
}
}
@Override
public void onClick(View source)
{
// 创建Intent
Intent intent = new Intent("org.crazyit.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.pre:
intent.putExtra("control",3);
case R.id.next:
intent.putExtra("control",4);
}
// 发送广播,将被Service组件中的BroadcastReceiver接收到
sendBroadcast(intent);
}
}
3. MusicService.java
这里是音乐播放服务,在此实现播放音乐的逻辑
package com.lonelyzhe.musicbox;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener
{
// 获取界面中显示歌曲标题、作者文本框
TextView title, author;
// 播放/暂停、停止按钮
ImageButton play, stop;
// 上一首,下一首按钮
ImageButton pre, next;
// 获取封面
ImageView cover;
ActivityReceiver activityReceiver;
public static final String CTL_ACTION =
"org.crazyit.action.CTL_ACTION";
public static final String UPDATE_ACTION =
"org.crazyit.action.UPDATE_ACTION";
// 定义音乐的播放状态,0x11代表没有播放;0x12代表正在播放;0x13代表暂停
int status = 0x11;
String[] titleStrs = new String[] { "心愿", "约定", "美丽新世界" };
String[] authorStrs = new String[] { "四个女生", "周蕙", "伍佰" };
Integer[] covers = new Integer[] { R.drawable.wish, R.drawable.promise, R.drawable.beautiful};
@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);
title = (TextView) findViewById(R.id.title);
author = (TextView) findViewById(R.id.author);
cover = findViewById(R.id.cover);
pre = this.findViewById(R.id.pre);
next = this.findViewById(R.id.next);
// 为两个按钮的单击事件添加监听器
play.setOnClickListener(this);
stop.setOnClickListener(this);
// 为上一首、下一首的单击事件添加监听器
pre.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]);
cover.setImageResource(covers[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;
}
}
}
@Override
public void onClick(View source)
{
// 创建Intent
Intent intent = new Intent("org.crazyit.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.pre:
intent.putExtra("control",3);
case R.id.next:
intent.putExtra("control",4);
}
// 发送广播,将被Service组件中的BroadcastReceiver接收到
sendBroadcast(intent);
}
}
4. AndroidManifest.xml
重要的一步,绑定MusicService
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lonelyzhe.musicbox">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MusicService">
</service> <!--这里要绑定服务-->
</application>
</manifest>
五、注意
1. assets和raw的区别
(1)raw目录下不能创建子目录
(2)asset下可以创建子目录,实现资源管理,更方便