service就是后台服务,不同于activity在前台,虽然用户看不见,但是作用是很大的。
我们在service中先需要实例化mediaplayer对象,这个在上节中已经讲了,除此之外我们需要注册一个广播接收器,用来接收用户操作的指令,比如上一曲,下一曲,暂停,播放等功能。
private String TAG = "MusicService";
private MediaPlayer player;
private MyBroadcastReceiver receiver;
private List<Mp3Info> mp3Infos;
private int position;
private boolean isFirst = true;
private int current;
@Override
public void onCreate() {
Log.i("music service", "oncreate");
super.onCreate();
regFilter();
}
注册广播时有一个属性为setPriority,设置是设置广播的优先级,从-1000到1000,值越大优先级越高,这里之所以需要设置这个优先级,是因为在做随机播放的时候,发现我先给一个随机方法取得播放列表的position,为了这个值能让每个activity,以及notification和service都取到,我设置了方法在application中,但是结果是每个页面有自己的随机数,播放的音乐与显示的音乐信息并不匹配,于是想到了用有序的方式来取值,将调用随机数的方法设置在service中,设置广播优先级最高,其他的优先级设置比这个低,这样只设置一次值,其他的从application中取就可以了。
/*
* 注册广播
*/
private void regFilter() {
IntentFilter filter = new IntentFilter();
filter.addAction(Constants.ACTION_LIST_SEARCH);
filter.addAction(Constants.ACTION_PAUSE);
filter.addAction(Constants.ACTION_PLAY);
filter.addAction(Constants.ACTION_NEXT);
filter.addAction(Constants.ACTION_PRV);
filter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
filter.setPriority(1000);
receiver = new MyBroadcastReceiver();
registerReceiver(receiver, filter); // 注册接收
}
广播的接收方法,通过intent的action来判断并处理
/*
* 创建自定义的广播接收器
*/
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Constants.ACTION_LIST_SEARCH)) {
Log.i("---" + TAG, "action_list_search");
long id = intent.getLongExtra("id", 0);
for (int i = 0; i < mp3Infos.size(); i++) {
if (id == mp3Infos.get(i).getId()) {
position = i;
prepareMusic(position);
isFirst = false;
break;
}
}
} else if (intent.getAction().equals(Constants.ACTION_PAUSE)) {
Log.i("---" + TAG, "action_pause");
if (player.isPlaying()) {
pauseMusic();
}
} else if (intent.getAction().equals(Constants.ACTION_PLAY)) {
Log.i("---" + TAG, "action_play");
if (!player.isPlaying()) {
if (isFirst) {
position = intent.getIntExtra("position", 0);
prepareMusic(position);
isFirst = false;
} else {
player.seekTo(current);
player.start();
}
}
} else if (intent.getAction().equals(Constants.ACTION_NEXT)) {
Log.i("---" + TAG, "action_next");
if ((Myapp.state % 3) == 1 || ((Myapp.state % 3) == 2)) {
if (position < mp3Infos.size() - 1) {
++position;
prepareMusic(position);
} else {
position = 0;
prepareMusic(0);
}
} else if ((Myapp.state % 3) == 0) {
Myapp.getRandom();
position = Myapp.position;
prepareMusic(position);
}
} else if (intent.getAction().equals(Constants.ACTION_PRV)) {
Log.i("---" + TAG, "action_prv");
if ((Myapp.state % 3) == 1 || ((Myapp.state % 3) == 2)) {
if (position == 0) {
position = mp3Infos.size() - 1;
prepareMusic(position);
} else {
--position;
prepareMusic(position);
}
} else if ((Myapp.state % 3) == 0) {
Myapp.getRandom();
position = Myapp.position;
prepareMusic(position);
}
} else if (intent.getAction().equals(
AudioManager.ACTION_AUDIO_BECOMING_NOISY)) {
//如果耳机拨出时暂停播放
if (intent.getIntExtra("state", 0) == 0 ) {
Intent intent2 = new Intent();
intent2.setAction(Constants.ACTION_PAUSE);
sendBroadcast(intent2);
}
}
}
}
当然了,在service被清除的时候需要释放资源,包括解绑注册的广播,回收mediaplayer的对象
@Override
public void onDestroy() {
Log.i("music service", "ondestroy");
super.onDestroy();
if (receiver != null) {
unregisterReceiver(receiver); // 服务终止时解绑
}
if (player != null) {
player.release();
player = null;
}
}