广播和服务之编写音乐盒代码
作业要求:参考Musicbox,添加音乐播放的 上一首 和 下一首 控制。
具体功能实现:
1.点击播放键播放音乐,再次点击则暂停,再点击则继续播放,点击停止则终止。
2.点击上下两个键可以进行切歌。
3.播放音乐时显示作者和曲名。
实现页面展示:
1打开:
2播放:
3切歌:
我们这次需要添加切歌功能,因此需要先下载对应切歌的两张图片,我找了半天最后自己做了一个,有需要的可以拖到桌面上,修改文件名称。再添加到drawable里面。
接着修改xml文件,分别将两张图片插入进去。下面是布局的完整代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/last"
android:layout_width="84dp"
android:layout_height="match_parent"
android:background="@drawable/last" />
<ImageButton
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="match_parent"
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="77dp"
android:layout_height="match_parent"
android:background="@drawable/next" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="#9C27B0"
android:ellipsize="marquee"
android:layout_weight="1"
android:marqueeRepeatLimit="marquee_forever"/>
<TextView
android:id="@+id/author"
android:textSize="25sp"
android:gravity="center_vertical"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
接着在mainactivity里连接xml里的last和next,再添加到onclick点击事件的case里,添加监听完整代码如下
package com.example.xr.musicbox;
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