最近在一直在弄畢設(shè),是一個(gè)音樂播放器训措。使用的是原生的MediaPlayer,下面是開原地址:https://github.com/Andy-86/FantasticPlayer
因?yàn)橐婚_始是播放本地音樂,所以一直使用MediaPlayer.prepare();但當(dāng)播放在線音樂的時(shí)候會(huì)出現(xiàn)阻塞芹啥,所以使用MediaPlayer.prepareAsync();,但是有時(shí)候會(huì)發(fā)生錯(cuò)誤(-38,0)铺峭。最后查閱官方文檔才發(fā)現(xiàn)當(dāng)再Prepare的時(shí)候假如有stop()墓怀,start(),setDataSource()卫键,seekTo(progress)傀履,pause(),getCurrentPosition();等都會(huì)報(bào)錯(cuò)永罚,所以要像中斷操作只能使用reset()方法啤呼。
下面是我的Mediaplayer的Service的代碼,我是通過AIDl進(jìn)行控制和將信息廣播出去呢袱,由于ADIL運(yùn)行在Binder線程池里面 使用都是用了synchronize來同步官扣。
public class MusicService extends Service implements MediaPlayer.OnErrorListener,
MediaPlayer.OnPreparedListener,
MediaPlayer.OnCompletionListener,
AudioManager.OnAudioFocusChangeListener {
public static final int MUSIC_ACTION_PLAY = 0xff001;
public static final int MUSIC_ACTION_LAST = 0xff002;
public static final int MUSIC_ACTION_NEXT = 0xff003;
public static final int MUSIC_ACTION_PAUSE = 0xff004;
public static final int MUSIC_ACTION_STOP = 0xff005;
public static final int MUSIC_ACTION_CONTINUE_PLAY = 0xff006;
public static final int MUSIC_ACTION_SEEK_PLAY = 0xff007;
public static final int MUSIC_ACTION_COMPLETE = 0xff008;
public static final int MUSIC_ACTION_ERROR=0xff009;
public static final String SONGBEAN="Songbean";
private RemoteCallbackList<MusicPlayListner> remoteCallbackList = new RemoteCallbackList<>();
private Timer timer;
private static final String TAG="MusicService";
private SongBean songBean;
@Override
public void onCreate() {
init();
super.onCreate();
}
private IBinder mybinder = new IMusicPlayer.Stub()
{
@Override
public void action(int actioncode, SongBean songBean) throws RemoteException {
switch (actioncode) {
case MUSIC_ACTION_PLAY:
String path;
setSongBean(songBean);
if(songBean.getM4a()!=null)
path = songBean.getM4a();
else
path = songBean.getPath();
playSong(path);
LogUtil.doLog("String path", "paly:" + path);
Message msg=Message.obtain();
msg.what=MUSIC_ACTION_PLAY;
sendMessge(MUSIC_ACTION_PLAY,msg);
break;
case MUSIC_ACTION_PAUSE:
pauseSong();
LogUtil.doLog("action", "pause");
Message msg2=Message.obtain();
msg2.what=MUSIC_ACTION_PAUSE;
sendMessge(MUSIC_ACTION_PAUSE,msg2);
break;
case MUSIC_ACTION_STOP:
stopSong();
break;
case MUSIC_ACTION_LAST:
stopSong();
setSongBean(songBean);
String path2 = (String) songBean.getM4a();
playSong(path2);
break;
case MUSIC_ACTION_NEXT:
stopSong();
setSongBean(songBean);
String path3 = (String) songBean.getM4a();
playSong(path3);
break;
case MUSIC_ACTION_CONTINUE_PLAY:
continuePlaySong();
LogUtil.doLog("action", "continue play");
Message msg3=Message.obtain();
msg3.what=MUSIC_ACTION_CONTINUE_PLAY;
sendMessge(MUSIC_ACTION_CONTINUE_PLAY,msg3);
break;
case MUSIC_ACTION_SEEK_PLAY:
int progerss=songBean.getProgress();
seekPlaySong(progerss);
break;
}
}
@Override
public void registListner(MusicPlayListner listner) throws RemoteException {
remoteCallbackList.register(listner);
}
@Override
public void unregistListner(MusicPlayListner listner) throws RemoteException {
remoteCallbackList.unregister(listner);
}
};
public MusicService() {
}
private MediaPlayer mMediaPlayer;
private MediaPlayer onlinePlayer;
private Handler handler;
public void init() {
handler = new Handler();
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnErrorListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnCompletionListener(this);
((AudioManager) getSystemService(Context.AUDIO_SERVICE)).
requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}
public void initOnlinePlyer(){
onlinePlayer=new MediaPlayer();
onlinePlayer.setOnErrorListener(this);
onlinePlayer.setOnPreparedListener(this);
onlinePlayer.setOnCompletionListener(this);
}
@Override
public void onAudioFocusChange(int focusChange) {
}
@Override
public void onCompletion(MediaPlayer mp) {
Message msg=Message.obtain();
msg.what=MUSIC_ACTION_COMPLETE;
sendMessge(MUSIC_ACTION_COMPLETE,msg);
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
LogUtil.doLog("onError",""+what+extra);
if(songBean.getM4a()!=null) {
Message message = Message.obtain();
Bundle bundle = new Bundle();
bundle.putParcelable("SongBean", songBean);
message.setData(bundle);
message.what = MUSIC_ACTION_ERROR;
sendMessge(MUSIC_ACTION_ERROR, message);
}
mMediaPlayer.reset();
return false;
}
@Override
public void onPrepared(MediaPlayer mp) {
mMediaPlayer.start();
if (timer == null)
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
updateProgress();
}
},0, 1000);
}
/**
* 更新進(jìn)度
*/
private synchronized void updateProgress() {
if(mMediaPlayer.isPlaying()) {
Message msg = Message.obtain();
msg.arg1 = mMediaPlayer.getCurrentPosition();
msg.arg2 = mMediaPlayer.getDuration();
msg.what = MUSIC_ACTION_SEEK_PLAY;
LogUtil.doLog("updateProgress", "" + mMediaPlayer.getCurrentPosition());
sendMessge(MUSIC_ACTION_PLAY, msg);
}
}
/**
* 向訂閱的Listner發(fā)送Message
*
* @param msg
*/
private synchronized void sendMessge(int action,Message msg) {
int n = remoteCallbackList.beginBroadcast();
for (int i = 0; i < n; i++) {
MusicPlayListner listner = remoteCallbackList.getBroadcastItem(i);
try {
listner.action(action, msg,songBean);
} catch (RemoteException e) {
e.printStackTrace();
}
}
remoteCallbackList.finishBroadcast();
}
public synchronized void playSong(String path) {
try {
if(songBean.getM4a()!=null)
{
Uri uri=Uri.parse(path);
mMediaPlayer.reset();//idle
mMediaPlayer.setDataSource(this,uri);
mMediaPlayer.prepareAsync();
return;
}
mMediaPlayer.reset();//idle
mMediaPlayer.setDataSource(path);
mMediaPlayer.prepareAsync();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public synchronized void stopSong() {
if (mMediaPlayer != null) {
mMediaPlayer.stop();
}
}
public synchronized void seekPlaySong(int progress) {
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
mMediaPlayer.seekTo(progress);
}
}
public synchronized void pauseSong() {
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
}
}
public synchronized void continuePlaySong() {
if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
mMediaPlayer.start();
}
}
@Override
public IBinder onBind(Intent intent) {
LogUtil.doLog("onBind", "return binder-------------------");
return mybinder;
}
public void setSongBean(SongBean songBean) {
this.songBean = songBean;
}
}