Android Studio音乐播放器
一、本项目完成的功能
1.实现音乐播放器的基本功能:播放音乐,暂停音乐,停止音乐,切换到上一首或者下一首音乐
2.实现一些进阶功能:显示专辑封面、歌曲名称、歌手姓名和进度条
3.调整不同控件的位置、大小,使得整个软件界面看起来大方美观
二、代码部分
1.基本代码
在MainActivity中,初始化一些必要的值,例如音乐播放状态和歌曲名称列表。
public class MainActivity extends Activity implements OnClickListener {
// 获取界面中显示歌曲标题、作者文本框
TextView title, author;
// 播放/暂停、停止按钮
ImageButton play, stop;
// 上一首,下一首按钮
ImageButton pre, next;
// 获取封面
ImageView cover;
ActivityReceiver activityReceiver;
public static SeekBar audioSeekBar = null;
public static final String CTL_ACTION =
"org.ly.action.CTL_ACTION";
public static final String UPDATE_ACTION =
"org.ly.action.UPDATE_ACTION";
// 定义音乐的播放状态,0x11代表没有播放;0x12代表正在播放;0x13代表暂停
int status = 0x11;
String[] titleStrs = new String[] {
"Ride", "Intentions", "thank u, next" };
String[] authorStrs = new String[] {
"Lana Del Rey", "Justin Bieber", "Ariana Grande" };
Integer[] covers = new Integer[] {
R.drawable.lana, R.drawable.bieber, R.drawable.ariana};
在onCreate函数中,获取页面中各种控件,添加监听器。
@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)