当启动一个Service的时候,会调用该Service中的onCreate()和onStartCommand()方法。
当再次start Service,这次只有onStartCommand()方法执行了,onCreate()方法并没有执行,为什么会这样呢?
这是由于onCreate()方法只会在Service第一次被创建的时候调用,如果当前Service已经被创建过了,不管怎样调用startService()方法,onCreate()方法都不会再执行。
所以,我们可以通过startIntent.putExtra(“String”, String);传递Action开启服务。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
acquireWakeLock();
if (intent!=null){
switch (intent.getStringExtra("action")) {
case START:
MusicUtil.getInstance().prePlayOrNextPlay();
musicNotifi.onUpdataMusicNotifi(MusicUtil.getInstance().getNewSongInfo(),MusicUtil.getInstance().isPlaying());
sendBroadcast(intent2);
break;
case COMPLETE:
MusicUtil.getInstance().prePlayOrNextPlay();
musicNotifi.onUpdataMusicNotifi(MusicUtil.getInstance().getNewSongInfo(),MusicUtil.getInstance().isPlaying());
sendBroadcast(intent2);
break;
case PLAYORPAUSE:
MusicUtil.getInstance().playOrPause();
musicNotifi.onUpdataMusicNotifi(MusicUtil.getInstance().getNewSongInfo(),MusicUtil.getInstance().isPlaying());
sendBroadcast(intent2);
break;
case PREVIOUSMUSIC:
MusicUtil.getInstance().prePlayOrNextPlay();
musicNotifi.onUpdataMusicNotifi(MusicUtil.getInstance().getNewSongInfo(),MusicUtil.getInstance().isPlaying());
sendBroadcast(intent2);
break;
case NEXTMUSIC:
MusicUtil.getInstance().prePlayOrNextPlay();
musicNotifi.onUpdataMusicNotifi(MusicUtil.getInstance().getNewSongInfo(),MusicUtil.getInstance().isPlaying());
sendBroadcast(intent2);
break;
}
}
public class MusicUtil{
private int currentSongPosition = -1;
private boolean isPrepare = false;
private static MediaPlayer mediaPlayer;
private static MusicUtil musicUtils;
private List list;//本地歌曲数据
/**
* 顺序播放
*/
public static final int TYPE_ORDER = 4212;
/**
* 播放模式
*/
private int pattern = TYPE_ORDER;
/**
* 随机播放
*/
public static final int TYPE_RANDOM = 4313;
/**
* 单曲循环
*/
public static final int TYPE_SINGLE = 4414;
public Song song;
public int getPattern(){
return pattern;
}//获得当前播放模式
public void setPatten(int i) {
if (i == TYPE_SINGLE) {
this.pattern = TYPE_SINGLE;
}
if (i == TYPE_ORDER) {
this.pattern = TYPE_ORDER;
Log.i("pat8",""+pattern);
}
if (i == TYPE_RANDOM) {
this.pattern = TYPE_RANDOM;
}
}//设置当前播放模式
public int getCurrentSongPosition(){
return currentSongPosition;
}//获得当前播放歌曲的下标
//播放还是暂停
public void playOrPause() {
if (mediaPlayer == null) {
playMusic(list.get(0));
return;
}
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else {
mediaPlayer.start();
}
}
//释放资源
public void clean() {
mediaPlayer.stop();
mediaPlayer.release();
}
//根据播放模式 将上一首歌歌曲改成当前播放歌曲下标
public void pre(){
preSongPosition = currentSongPosition;
if (pattern == TYPE_ORDER||pattern == TYPE_SINGLE) {
preSongPosition = currentSongPosition;
if (currentSongPosition == 0){
currentSongPosition=MusicUtil.getInstance().getListCount()-1;
}else{
currentSongPosition = currentSongPosition -1;
}
}
if (pattern == TYPE_RANDOM) {
currentSongPosition = new Random().nextInt(MusicUtil.getInstance().getListCount()-1);
}
Log.i("song1",""+list.get(currentSongPosition).getSinger());
}
//播放
public void prePlayOrNextPlay(){
playMusic(list.get(currentSongPosition));
}
//将新的歌曲信息返回 用于界面数据的更新
public Song getNewSongInfo(){
if(list.get(currentSongPosition)==null)
Log.d(TAG, "getNewSongInfo: "+"孔");
return list.get(currentSongPosition);
}
public Song getPreSongInfo(){
if(list.get(currentSongPosition)==null)
Log.d(TAG, "getNewSongInfo: "+"孔");
return list.get(preSongPosition);
}
// 下一首,和Pre()相对应
public void next(){
preSongPosition = currentSongPosition;
if (pattern == TYPE_ORDER||pattern == TYPE_SINGLE) {
if (currentSongPosition == MusicUtil.getInstance().getListCount()-1){
currentSongPosition = 0;
}else{
currentSongPosition=currentSongPosition+1;
}
}
if (pattern == TYPE_RANDOM) {
currentSongPosition = new Random().nextInt(MusicUtil.getInstance().getListCount()-1);
}
}
//完成当前歌曲后执行下一首 与pre不同的是 若为单曲循环 则当前歌曲不变
public void comNext(){
preSongPosition = currentSongPosition;
if (pattern == TYPE_SINGLE){
return;
}else if (pattern == TYPE_ORDER) {
if (currentSongPosition == MusicUtil.getInstance().getListCount()-1){
currentSongPosition = 0;
}else{
currentSongPosition=currentSongPosition+1;
}
}else if (pattern == TYPE_RANDOM) {
currentSongPosition = new Random().nextInt(MusicUtil.getInstance().getListCount()-1);
}
}
//获得mediaPlayer,方便后面调用其中方法时候使用
public MediaPlayer getMediaPlayer(){
return mediaPlayer;
}
}
if (song.getPosition() != MusicUtil.getInstance().getCurrentSongPosition()) {
LrcCount = 1;
MusicUtil.getInstance().setCurrentSongPosition(song.getPosition());
Intent startIntent6 = new Intent(this, MusicService.class);
startIntent6.putExtra("action", MusicService.START);
startService(startIntent6);
isplay = true;
} else {
isplay = MusicUtil.getInstance().isPlaying();
}
这里要注意,我是先改变Music Util里面的currentSongPosition值,再通过开启服务播放音乐,为什么不都放在服务里呢?
我不是故意多写的嘛~那是因为开启服务会有异步问题~我们会发现界面信息是之前那首~所以宝宝们写的时候要注意了!喵~
@OnClick({R.id.iv_back, R.id.iv_mode, R.id.iv_prev, R.id.iv_play, R.id.iv_next})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.iv_back:
break;
case R.id.iv_prev:
if (!isplay){
isplay=true;
ivPlay.setImageResource(R.drawable.play_btn_pause_selector);
}
MusicUtil.getInstance().pre();
changInfo();
Intent startIntent2 = new Intent(this, MusicService.class);
startIntent2.putExtra("action",MusicService.PREVIOUSMUSIC);
startService(startIntent2);
break;
case R.id.iv_play:
Intent startIntent1 = new Intent(this, MusicService.class);
startIntent1.putExtra("action",MusicService.PLAYORPAUSE);
startService(startIntent1);
if (isplay){
ivPlay.setImageResource(R.drawable.play_btn_play_selector);
isplay =false;
}else
{
isplay =true;
ivPlay.setImageResource(R.drawable.play_btn_pause_selector);
}
break;
case R.id.iv_next:
MusicUtil.getInstance().next();
changInfo();
if (!isplay){
isplay=true;
ivPlay.setImageResource(R.drawable.play_btn_pause_selector);
}
Intent startIntent3 = new Intent(this, MusicService.class);
startIntent3.putExtra("action",MusicService.NEXTMUSIC);
startService(startIntent3);
break;
}
}
MusicUtil.getInstance().getMediaPlayer().setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
MusicUtil.getInstance().comNext();
changInfo();//改变当前页面的信息
Intent startIntent4 = new Intent(MusicActivity.this, MusicService.class);
startIntent4.putExtra("action", MusicService.COMPLETE);
startService(startIntent4);
}
});
public void changInfo(){
Song newSong = MusicUtil.getInstance().getNewSongInfo();
if(newSong ==null)
return;
Log.i("song2",""+newSong.getSinger());
tvTitle.setText(newSong.getSong());
tvSinger.setText(newSong.getSinger());
}
private int mode = MusicUtil.TYPE_ORDER ;
设置播放模式为上次退出时候一样
if (mode != MusicUtil.getInstance().getPattern()) {
mode = MusicUtil.getInstance().getPattern();
}
// 根据模式设置相对应显示的图标
if (mode == MusicUtil.TYPE_ORDER) {
ivMode.setImageResource(R.drawable.play_btn_loop_selector);
} else if (mode == TYPE_SINGLE) {
ivMode.setImageResource(R.drawable.play_btn_one_selector);
} else {
ivMode.setImageResource(R.drawable.play_btn_shuffle_selector);
}
}
case R.id.iv_mode:
if (mode == MusicUtil.TYPE_ORDER){
Toast.makeText(this, "咦,已经切换为随机模式n(*≧▽≦*)n", Toast.LENGTH_SHORT).show();
mode = MusicUtil.TYPE_RANDOM;
ivMode.setImageResource(R.drawable.play_btn_shuffle_selector);
MusicUtil.getInstance().setPatten(MusicUtil.TYPE_RANDOM);
}else if(mode == MusicUtil.TYPE_RANDOM){
Log.i("pat2",""+mode);
Toast.makeText(this, "已经准备好单曲循环O(∩_∩)O", Toast.LENGTH_SHORT).show();
this.mode = TYPE_SINGLE;
ivMode.setImageResource(R.drawable.play_btn_one_selector);
MusicUtil.getInstance().setPatten(TYPE_SINGLE);
Log.i("pat4",""+mode);
}else if (mode == TYPE_SINGLE){
Log.i("pat5",""+mode);
Toast.makeText(this, "那就按顺序播放吧n(*≧▽≦*)n", Toast.LENGTH_SHORT).show();
this.mode = MusicUtil.TYPE_ORDER;
ivMode.setImageResource(R.drawable.play_btn_loop_selector);
MusicUtil.getInstance().setPatten(MusicUtil.TYPE_ORDER);
Log.i("pat6",""+mode);
}
break;