MediaPlayer 播放网络音频工具类

最近的项目需要播放网络音频,就直接摘成一个工具类了

public class MediaPlayerUtil {
  private static volatile MediaPlayerUtil mediaPlayer;

  private MediaPlayerUtil() {
  }

  public static MediaPlayerUtil getMediaPlayer() {
    if (mediaPlayer == null) {
        synchronized (MediaPlayerUtil.class) {
            if (mediaPlayer == null) {
                mediaPlayer = new MediaPlayerUtil();
            }
        }
    }
    return mediaPlayer;
}

private MediaPlayer mPlayer;

/**
 * 播放音频
 *
 * @param songUrl 网络音频Url
 */
public void player(String songUrl) {

    if (TextUtils.equals(songUrl, nowPlaySongUrl)) {
        Log.d(TAG, "player: 重复的url");
        return;
    }
    stopPlay();
    nowPlaySongUrl = songUrl;

    if (mPlayer == null) {
        mPlayer = new MediaPlayer();
    }
    try {
        Log.d(TAG, "player: 当前要播放的歌曲Url === " + songUrl);

        mPlayer.reset();
        //====这种方式只能http,https会抛IO异常
        mPlayer.setDataSource(songUrl);

        mPlayer.prepareAsync();
        mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                Log.d(TAG, "onPrepared: 播放 "+mp.getDuration());
                mp.start();
            }
        });
        mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                nowPlaySongUrl = "";
                if (mPlayer != null)
                    mPlayer.release();
                if (mPlayer != null)
                    mPlayer = null;
            }
        });
    } catch (IOException e) {
        Log.d(TAG, " 播放音乐异常" + e.getMessage());
        ToastUtil.showShort(" 播放音乐异常" + e.getMessage());
    }
}

/**
 * 列表中的音频播放
 * 

* 点击播放,再点击停止 * * @param songUrl */ public void playOrStop(String songUrl) { if (mPlayer != null) { stopPlay(); } else { player(songUrl); } } /** * 停止播放音频,lastSongUrl置空 */ public void stopPlay() { nowPlaySongUrl = ""; try { if (mPlayer != null) mPlayer.stop(); if (mPlayer != null) mPlayer.release(); if (mPlayer != null) mPlayer = null; } catch (Exception e) { Log.e(TAG, "stopPlay: " + e.toString()); } } // 当前播放的音频地址 private static String nowPlaySongUrl; private static String TAG = "TAG.Util.MediaPlay"; }

你可能感兴趣的:(MediaPlayer 播放网络音频工具类)