日期:2014-05-20 浏览次数:20784 次
if(myMediaPlayer!=null){
System.out.println("myMediaPlayer!=null");
myMediaPlayer.stop();//stop???看下MediaPlayer状态图吧
}
public void play(MediaListItem item) {
resetMediaPlayer();
String path = item.getMediaPath();
if (!SystemUtil.isMediaFileExisted(path)) {
// 删除该文件,播放下一首
}
/** 设置当前播放的媒体文件资源 */
try {
mediaPlayer.setDataSource(path);
mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
CLog.d(String.format("播放%s失败", item.getMediaDisplayName()));
e.printStackTrace();
} catch (SecurityException e) {
CLog.d(String.format("播放%s失败", item.getMediaDisplayName()));
e.printStackTrace();
} catch (IllegalStateException e) {
CLog.d(String.format("播放%s失败", item.getMediaDisplayName()));
e.printStackTrace();
} catch (IOException e) {
CLog.d(String.format("播放%s失败", item.getMediaDisplayName()));
e.printStackTrace();
}
}
/**
* 重置播放器
*/
private void resetMediaPlayer() {
/* 如果MediaPlayer不为空,将其停止,释放资源并强制置为null */
releaseMediaPlayer();
mediaPlayer = new MediaPlayer();
/* 设置监听事件 */
setMediaPlayerListener();
}
private void setMediaPlayerListener() {
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnCompletionListener(this);
}
public void releaseMediaPlayer() {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}