Android-工作遭遇-音视频播放控制篇(2)

此篇主要是讲Android的视频播放

在android播放视频,无非就是videoview或者mediaplay.

但是查看videoview的源码之后,发现.他也是基于mediaplay进行播放的.就决定自己封装一个mediaplay.用于日常使用.

首先看下MediaPlay的介绍.我的另外一篇文章讲述到了音频的播放.

Android-工作遭遇-音量音频播放控制篇(1)

Android的多媒体框架支持各种常见的多媒体类型,这样在程序中可以很容易地集成音频、视频或者图片。Android下对于音频或者视频的支持均需要使用到的类

 主要核心.setDataSource.在android 21之后,meidiaplay加了网络播放视频的原则.

 此处header不允许为null,所以我这边传了new了一个hashmap


                mMediaPlayer.setDataSource(mContext, Uri.parse(mPath), new HashMap<String, String>());

                mMediaPlayer.setDataSource(mPath);

另外一个核心,异步准备. 


                mMediaPlayer.prepareAsync();

不过这个方法有点坑,跟ijkplay不一样.此方法之后,想要start,需要在其的监听回调用,进行播放.

防止mediaplay多次初始化

if (mMediaPlayer == null) {
            mMediaPlayer = new MediaPlayer();
            if (listener != null) {
                mMediaPlayer.setOnPreparedListener(listener);
                mMediaPlayer.setOnInfoListener(listener);
                mMediaPlayer.setOnSeekCompleteListener(listener);
                mMediaPlayer.setOnBufferingUpdateListener(listener);
                mMediaPlayer.setOnErrorListener(listener);
                mMediaPlayer.setOnVideoSizeChangedListener(listener);
                mMediaPlayer.setOnCompletionListener(listener);
            }

        }

每次切换之后产生黑屏问题,主要是需要设置display.这个非常关键.如果不设置,切换之后回来会是黑屏

 //给mediaPlayer设置视图
        mMediaPlayer.setDisplay(surfaceView.getHolder());

附上全部代码,


public class VideoView extends FrameLayout {
 
    private MediaPlayer mMediaPlayer = null;

    /**
     * 视频文件地址
     */
    private String mPath = "";

    private SurfaceView surfaceView;

    private VideoPlayerListener listener;
    private Context                mContext;
    //    private SharedPreferences   mVideoViewConfig;
    private boolean isFrist = true;
    private boolean isURL;

    public NeoAdVideoView(@NonNull Context context) {
        super(context);
        initVideoView(context);
    }

    public NeoAdVideoView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initVideoView(context);
    }

    public NeoAdVideoView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initVideoView(context);
    }

    private void initVideoView(Context context) {
        this.setBackgroundColor(Color.BLACK);
        mContext = context;
//        mVideoViewConfig = context.getSharedPreferences("VideoViewConfig", Context.MODE_PRIVATE);
//获取焦点
        setFocusable(true);
    }

    /**
     * 设置视频地址。
     * 根据是否第一次播放视频,做不同的操作。
     *
     * @param path the path of the video.
     */
    public void setVideoPath(String path, boolean isURL) {
        this.isURL = isURL;
        if (TextUtils.equals("", mPath)) {
            //如果是第一次播放视频,那就创建一个新的surfaceView
            mPath = path;
            createSurfaceView();
        } else {
            //否则就直接load
            mPath = path;
            load();
        }

    }

    /**
     * 设置视频地址。
     * 根据是否第一次播放视频,做不同的操作。
     *
     * @param path the path of the video.
     */
    public void setVideoPath(File path) {
        setVideoPath(path.getAbsolutePath(), false);
    }

    /**
     * 新建一个surfaceview
     */
    public void createSurfaceView() {
        //生成一个新的surface view
        surfaceView = new SurfaceView(mContext);
        surfaceView.getHolder().addCallback(new SurfaceCallback());
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT
                , LayoutParams.MATCH_PARENT, Gravity.CENTER);
        surfaceView.setLayoutParams(layoutParams);
//        surfaceView.setZOrderOnTop(true);
        this.addView(surfaceView);
    }

    public int getVideoWidth() {
        if (mMediaPlayer != null) {
            return mMediaPlayer.getVideoWidth();
        }
        return 0;
    }

    public int getVideoHeight() {
        if (mMediaPlayer != null) {
            return mMediaPlayer.getVideoHeight();
        }
        return 0;
    }

    public boolean isPlaying() {
        return mMediaPlayer.isPlaying();
    }

    /**
     * surfaceView的监听器
     */
    private class SurfaceCallback implements SurfaceHolder.Callback {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            load();
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
//            System.out.println("播放");
            //surfaceview创建成功后,加载视频
            start();


        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            pause();
        }
    }

    /**
     * 加载视频
     */
    public void load() {
        //每次都要重新创建IMediaPlayer
        createPlayer();
        //setBasic();
        try {
            if (isURL) {
                mMediaPlayer.setDataSource(mContext, Uri.parse(mPath), new HashMap<String, String>());
            } else {
                mMediaPlayer.setDataSource(mPath);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        //给mediaPlayer设置视图
        mMediaPlayer.setDisplay(surfaceView.getHolder());
        if (isFrist) {
            if (isURL) {
                mMediaPlayer.prepareAsync();
            } else {
                try {
                    mMediaPlayer.prepare();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            isFrist = false;
        }
    }


    /**
     * 创建一个新的player
     */
    public void createPlayer() {
        if (mMediaPlayer == null) {
            mMediaPlayer = new MediaPlayer();
            if (listener != null) {
                mMediaPlayer.setOnPreparedListener(listener);
                mMediaPlayer.setOnInfoListener(listener);
                mMediaPlayer.setOnSeekCompleteListener(listener);
                mMediaPlayer.setOnBufferingUpdateListener(listener);
                mMediaPlayer.setOnErrorListener(listener);
                mMediaPlayer.setOnVideoSizeChangedListener(listener);
                mMediaPlayer.setOnCompletionListener(listener);
            }

        }
    }


    public void setListener(VideoPlayerListener listener) {
        this.listener = listener;
        if (mMediaPlayer != null) {
            mMediaPlayer.setOnPreparedListener(listener);
        }
    }

    /**
     * -------======------- 下面封装了一下控制视频的方法
     */
    public void start() {
        if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
            mMediaPlayer.start();
            if (mOnPlayStatusChangeListener != null) {
                mOnPlayStatusChangeListener.onStatusChange(1);
            }
        }
    }

    public void release() {
        if (mMediaPlayer != null) {
            mMediaPlayer.reset();
            mMediaPlayer.setDisplay(null);
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    public void pause() {
        if (mMediaPlayer != null) {
            mMediaPlayer.pause();
            if (mOnPlayStatusChangeListener != null) {
                mOnPlayStatusChangeListener.onStatusChange(2);
            }
        }
    }

    public void stop() {
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
        }
    }


    public void reset() {
        if (mMediaPlayer != null) {
            mMediaPlayer.reset();
        }
    }


    public long getDuration() {
        if (mMediaPlayer != null) {
            return mMediaPlayer.getDuration();
        } else {
            return 0;
        }
    }

    public void toggle() {
        if (mMediaPlayer != null) {
            if (mMediaPlayer.isPlaying()) {
                pause();
            } else {
                start();
            }
        }
    }


    public long getCurrentPosition() {
        if (mMediaPlayer != null) {
            return mMediaPlayer.getCurrentPosition();
        } else {
            return 0;
        }
    }


    public synchronized void seekTo(int l) {
        if (mMediaPlayer != null) {
            mMediaPlayer.seekTo(l);
        }
    }


    private onPlayStatusChangeListener mOnPlayStatusChangeListener;

    protected void setOnPlayStatusChangeListener(onPlayStatusChangeListener onPlayStatusChangeListener) {
        mOnPlayStatusChangeListener = onPlayStatusChangeListener;
    }

    protected interface onPlayStatusChangeListener {
        void onStatusChange(int status);
    }

}

 

发布了73 篇原创文章 · 获赞 19 · 访问量 6万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览