第一个版本,应该有bug,请谨慎使用
package com.test.first_as_app.testplayer;
import android.media.MediaPlayer;
import android.text.TextUtils;
import android.util.Log;
import java.io.IOException;
public class OnlinePlayer {
private static OnlinePlayer Instance;
public static OnlinePlayer getInstance(){
if(Instance == null){
Instance = new OnlinePlayer();
}
return Instance;
}
private MediaPlayer mediaPlayer;
private final static int Idle = -1; // 未使用状态,或者 调用了stop()
private final static int Preparing = 0; // 正在加载声音中
private final static int Prepared = 1; // onPrepared 回调,或者 调用了 pause()
private final static int Playing = 2; // 正在播放
private final static int Completion = 3; // 播放完成
private int mStatus = Idle;
private boolean mPauseFlag;
private boolean mStopFlag;
private boolean mReleaseFlag;
private String mCurrentUrl;
private String mWaitUrl;
// private MediaPlayer.OnErrorListener mOutErrorListener;
private MediaPlayer.OnErrorListener mErrorListener = new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
mp.reset();
mStatus = Idle;
return true;
}
};
private MediaPlayer.OnPreparedListener mOutPreparedListener;
private MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener(){
@Override
public void onPrepared(MediaPlayer mp) {
mStatus = Prepared;
if(mReleaseFlag){
Log.e("lugx", "onPrepared release");
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
return;
}
if(mStopFlag){
Log.e("lugx", "onPrepared stop");
mStopFlag = false;
mediaPlayer.stop();
mStatus = Idle;
return;
}
if(mPauseFlag){
Log.e("lugx", "onPrepared pause");
mPauseFlag = false;
return;
}
if( TextUtils.isEmpty(mWaitUrl) ) {
mStatus = Playing;
mediaPlayer.start();
if(mOutPreparedListener != null){
mOutPreparedListener.onPrepared(mp);
}
}
else{
String url = mWaitUrl;
mCurrentUrl = null;
mWaitUrl = null;
play(url);
}
}
};
private MediaPlayer.OnCompletionListener mOutcompletionListener;
private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mStatus = Completion;
if(mOutcompletionListener != null){
mOutcompletionListener.onCompletion(mp);
}
}
};
public OnlinePlayer(){
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnErrorListener(mErrorListener);
}
private void preparePlay(){
if(mediaPlayer != null){
if( mediaPlayer.isPlaying() ){
mediaPlayer.stop();
}
mediaPlayer.reset();
}
}
public void play(String url, MediaPlayer.OnPreparedListener preparedListener,
MediaPlayer.OnCompletionListener completionListener){
this.mOutPreparedListener = preparedListener;
this.mCompletionListener = completionListener;
play(url);
}
public void play(String url){
if( TextUtils.isEmpty(url) || mReleaseFlag ||
mediaPlayer == null){
return;
}
mPauseFlag = false;
mStopFlag = false;
if(mStatus == Idle){
mCurrentUrl = url;
mWaitUrl = null;
playInner(url);
}
else if(mStatus == Preparing){
if( !mCurrentUrl.equals(url) ){
mWaitUrl = url;
}
}
else if(mStatus == Prepared || mStatus == Playing ||
mStatus == Completion){
if( url.equals(mCurrentUrl) ){
mStatus = Playing;
mediaPlayer.seekTo(0);
mediaPlayer.start();
}
else{
mCurrentUrl = url;
mWaitUrl = null;
playInner(url);
}
}
}
private void playInner(String url){
preparePlay();
mStatus = Preparing;
mediaPlayer.setOnPreparedListener(mPreparedListener);
mediaPlayer.setOnCompletionListener(mCompletionListener);
try {
mediaPlayer.setDataSource(url);
mediaPlayer.prepareAsync();
}catch(IllegalArgumentException e) {
Log.e("lugx", "playInner IllegalArgumentException");
e.printStackTrace();
}catch(IllegalStateException e){
Log.e("lugx", "playInner IllegalStateException");
e.printStackTrace();
} catch (IOException e) {
Log.e("lugx", "playInner IOException");
e.printStackTrace();
}
}
public void pause(){
if(mediaPlayer == null) {
return;
}
if(mStatus == Idle || mStatus == Prepared || mStatus == Completion){
mPauseFlag = false;
}
else if(mStatus == Preparing){
if( !mStopFlag ) {
mPauseFlag = true;
}
}
else if(mStatus == Playing){
mPauseFlag = false;
mediaPlayer.pause();
mStatus = Prepared;
}
}
public void start(){
if(mediaPlayer == null) {
return;
}
if(mStatus == Idle || mStatus == Playing){
mPauseFlag = false;
}
else if(mStatus == Prepared || mStatus == Completion){
mStatus = Playing;
mediaPlayer.start();
}
else if(mStatus == Preparing){
mPauseFlag = false;
}
}
public void seekTo(int msec){
if(mediaPlayer == null) {
return;
}
if(mStatus == Prepared || mStatus == Playing ||
mStatus == Completion){
mediaPlayer.seekTo(msec);
}
}
public boolean isPlaying(){
return mediaPlayer.isPlaying();
}
public int getPlayerStatus(){
return mStatus;
}
public void stop(){
if(mediaPlayer == null) {
return;
}
if(mStatus == Idle){
mStopFlag = false;
}
else if(mStatus == Preparing){
mStopFlag = true;
mPauseFlag = false;
}
else if(mStatus == Prepared || mStatus == Playing ||
mStatus == Completion){
mStopFlag = false;
mediaPlayer.stop();
mStatus = Idle;
mCurrentUrl = null;
mWaitUrl = null;
}
}
public void release(){
if(mediaPlayer == null) {
return;
}
stop();
if(mediaPlayer != null){
if(mStatus == Preparing){
mReleaseFlag = true;
}
else if(mStatus == Idle || mStatus == Prepared ||
mStatus == Playing || mStatus == Completion){
mediaPlayer.release();
mediaPlayer = null;
}
}
}
}