Android 本地音频播放工具类 避免重复播放 实现连续播放

很简单, 直接上代码:

调用方式:
 

1.
        // 播放铃声
        MediaPlayUtil.setPlayFlag(true);
        MediaPlayUtil.playMp3(MainApplication.getContext(), R.raw.socket_change, null);




2.
    static int mediaPlayResourceId = -1;
           // 播放铃声
                mediaPlayResourceId = R.raw.xx_00;
                MediaPlayUtil.setPlayFlag(true);
                MediaPlayUtil.playMp3(MainApplication.getContext(), R.raw.xx_00, mMediaPlayUtilCallBack);


static MediaPlayUtil.CallBack mMediaPlayUtilCallBack = new MediaPlayUtil.CallBack(){
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            switch (mediaPlayResourceId) {
                case R.raw.xx_00:
                    // 可以播放完毕再次播放
                        MediaPlayUtil.setPlayFlag(true);
                MediaPlayUtil.playMp3(MainApplication.getContext(), R.raw.xx_00, mMediaPlayUtilCallBack);
                    break;
                case R.raw.xx_01:
                      // 可以进行其他操作
                      break;
            }
        }
    };

3.
              // 停止播放
                MediaPlayUtil.stopMp3();

工具类:

package com.yutu.tv.whUtil;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;

public class MediaPlayUtil {

    public static final String TAG = "byWh";
    public static final String TAG2 = "MediaPlayUtil - ";
    public static CallBack mCallBack;
    private static MediaPlayer mMediaPlayer;
    private static boolean playFlag = true;

    public static void setPlayFlag(boolean mPlayFlag) {
        playFlag = mPlayFlag;
        doWhLog("setPlayFlag - playFlag = " + playFlag);
    }

    public static void playMp3(Context mContext, int mResourceId, CallBack nCallBack) {
        doWhLog("playMp3 - playFlag = " + playFlag);
        if (!playFlag) {
            return;
        }

        mCallBack = nCallBack;

        if (mMediaPlayer == null) {
            mMediaPlayer = new MediaPlayer().create(mContext, mResourceId);
        }
        try {
            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.stop();
                mMediaPlayer.release();
                mMediaPlayer = null;
                mMediaPlayer = new MediaPlayer().create(mContext, mResourceId);
            }
        }  catch (IllegalStateException e) {
            e.printStackTrace();
            doWhLog("playMp3 - new - Exception = " + e.getMessage());
            return;
        }
        try {
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mMediaPlayer) {
                    mMediaPlayer.start();
                }
            });
            // 播放本地音乐不需要这句
            // mMediaPlayer.prepareAsync();
        } catch (Exception e) {
            e.printStackTrace();
            doWhLog("playMp3 - start - Exception = " + e.getMessage());
        }
        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                doWhLog("onCompletion - playFlag = " + playFlag);
                mMediaPlayer.stop();
                mMediaPlayer.release();
                mMediaPlayer = null;
                if (mCallBack != null) {
                    mCallBack.onCompletion(mediaPlayer);
                }
            }
        });
    }

    public static void stopMp3() {
        doWhLog("stopMp3");
        playFlag = false;
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    // 内部接口
    public interface CallBack {
        void onCompletion(MediaPlayer mediaPlayer);
    }

    public static void doWhLog(String logInfo) {
        // SignLog.d(TAG, TAG2 + logInfo);
        Log.d(TAG, TAG2 + logInfo);
    }

}

觉得有用,请 点赞 + 关注,您的支持是我最大的动力!

觉得有用,请 点赞 + 关注,您的支持是我最大的动力!

觉得有用,请 点赞 + 关注,您的支持是我最大的动力!

你可能感兴趣的:(Android基础控件,Android,android,音视频)