主界面

按下开始

按下暂停

按下停止

下一首

上一首

代码
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="wrap_content"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><TextViewandroid:id="@+id/title"android:layout_width="match_parent"android:layout_height="48dp"android:layout_weight="1"android:ellipsize="marquee"android:marqueeRepeatLimit="marquee_forever"android:textColor="#000000"android:textSize="25dp" /><TextViewandroid:id="@+id/author"android:layout_width="match_parent"android:layout_height="43dp"android:layout_weight="1"android:gravity="center_vertical"android:textSize="15dp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><ImageButtonandroid:id="@+id/play"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/play" /><ImageButtonandroid:id="@+id/stop"android:layout_width="wrap_content"android:layout_height="match_parent"android:src="@drawable/stop" /></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"><ImageButtonandroid:id="@+id/pre"android:layout_width="wrap_content"android:layout_height="match_parent"android:src="@android:drawable/ic_media_previous" /><ImageButtonandroid:id="@+id/next"android:layout_width="wrap_content"android:layout_height="match_parent"android:src="@android:drawable/ic_media_next" /></LinearLayout></LinearLayout>
MainActivity
package com.example.mymusicbox;import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;public class MainActivity extends Activity implements View.OnClickListener {TextView title, author;ImageButton play, stop,pre,next;public AudioManager mAudioManager = null;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[] { "群星", "小野道ono" ,"洛天依"};@Overridepublic 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);pre=(ImageButton) this.findViewById(R.id.pre);next=(ImageButton) this.findViewById(R.id.next);// 为两个按钮的单击事件添加监听器play.setOnClickListener(this);stop.setOnClickListener(this);pre.setOnClickListener(this);next.setOnClickListener(this);activityReceiver = new ActivityReceiver();// 创建IntentFilterIntentFilter filter = new IntentFilter();// 指定BroadcastReceiver监听的Actionfilter.addAction(UPDATE_ACTION);// 注册BroadcastReceiverregisterReceiver(activityReceiver, filter);Intent intent = new Intent(this, MyMusicService.class);// 启动后台ServicestartService(intent);}// 自定义的BroadcastReceiver,负责监听从Service传回来的广播public class ActivityReceiver extends BroadcastReceiver {@Overridepublic 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;}}}@Overridepublic void onClick(View source) {// 创建IntentIntent 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);break;case R.id.next:intent.putExtra("control", 4);break;}// 发送广播,将被Service组件中的BroadcastReceiver接收到sendBroadcast(intent);}
}
MyMusicService
package com.example.mymusicbox;import java.io.IOException;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;
import android.widget.Toast;public class MyMusicService extends Service {MyReceiver serviceReceiver;AssetManager am;String[] musics = new String[] { "圣者遗物.mp3", "夜行少女.mp3", "权御天下.mp3" };public static MediaPlayer mPlayer;// 当前的状态,0x11 代表没有播放 ;0x12代表 正在播放;0x13代表暂停int status = 0x11;// 记录当前正在播放的音乐int current = 0;@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {am = getAssets();// 指定该BroadcastReceiver能匹配的Intent// 创建BroadcastReceiverserviceReceiver = new MyReceiver();// 创建IntentFilterIntentFilter filter = new IntentFilter();filter.addAction(MainActivity.CTL_ACTION);registerReceiver(serviceReceiver, filter);// 创建MediaPlayermPlayer = new MediaPlayer();// 为MediaPlayer播放完成事件绑定监听器mPlayer.setOnCompletionListener(new OnCompletionListener() // ①{@Overridepublic void onCompletion(MediaPlayer mp) {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]);}});super.onCreate();}public class MyReceiver extends BroadcastReceiver {@Overridepublic 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:mPlayer.stop();current--;current= Math.abs(current % 3);prepareAndPlay(musics[current]);status = 0x12;break;case 4:mPlayer.stop();current++;current= current % 3;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);}}private void prepareAndPlay(String music) {try {AssetFileDescriptor afd = am.openFd(music);mPlayer.reset();mPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());mPlayer.prepare();mPlayer.start();} catch (IOException e) {e.printStackTrace();}}}
源码如下
https://gitee.com/cold-rivers/Mymusicbox














