Android 音频播放超简单封装

作者: i小灰 | 来源:发表于2022-03-16 14:42 被阅读0次
    package com.baidu.shunba.face.helper;
    
    import android.content.Context;
    import android.media.AudioManager;
    import android.media.MediaPlayer;
    import android.media.MediaPlayer.OnCompletionListener;
    import android.media.MediaPlayer.OnErrorListener;
    
    /**
     * 声音播放
     */
    public class MediaPlayerUtil {
    
        private static MediaPlayer mPlayer;
        private static boolean isPause = false;
        private static volatile boolean isPlaying = false;
    
        public static boolean isPlaying() {
            return isPlaying;
        }
    
        /**
         * playSound 播放声音  例如 MediaPlayerUtil.playSound(this, R.raw.verify_error_passenger);//无效票
         * @param context
         * @param rawResId
         */
        public static void playSound(Context context, int rawResId) {
            playSound(context, rawResId, null);
        }
    
        public static void playSound(Context context, final int rawResId, final OnCompletionListener listener) {
            try {
                release();
    
                mPlayer = MediaPlayer.create(context, rawResId);
    
                mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mPlayer.setOnCompletionListener(new OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        release();
                        isPlaying = false;
                        if (listener != null) {
                            try {
                                listener.onCompletion(mediaPlayer);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
                mPlayer.setOnErrorListener(new OnErrorListener() {
                    @Override
                    public boolean onError(MediaPlayer mp, int what, int extra) {
                        mPlayer.reset();
                        isPlaying = false;
                        if (listener != null) {
                            try {
                                listener.onCompletion(null);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        return false;
                    }
                });
    
    //            mPlayer.prepare();
                mPlayer.start();
                isPlaying = true;
    
                isPause = false;
            } catch (Exception e) {
                if (listener != null) {
                    try {
                        listener.onCompletion(null);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }
        public static void pause() {
            if (mPlayer != null && mPlayer.isPlaying()) {
                mPlayer.pause();
                isPause = true;
            }
        }
    
        // 继续
        public static void resume() {
            if (mPlayer != null && isPause) {
                mPlayer.start();
                isPause = false;
            }
        }
    
        public static void release() {
            if (mPlayer != null) {
                try {
                    mPlayer.release();
                } catch (Exception e) {
    
                }
                mPlayer = null;
            }
    
            isPlaying = false;
        }
    }
    
    

    相关文章

      网友评论

        本文标题:Android 音频播放超简单封装

        本文链接:https://www.haomeiwen.com/subject/qpnbvltx.html