Android Open Source - AdvancedPlayer Super Video View






From Project

Back to project page AdvancedPlayer.

License

The source code is released under:

Apache License

If you think the Android project AdvancedPlayer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.huilan.library_videoplay.view;
/*from w ww .j  a v a 2  s. c  o m*/
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * ??SurfaceView ?????????,???
 * Created by liujigang on 2014/8/11 0011.
 */
public class SuperVideoView extends SurfaceView {

    private final Context mContext;
    private SurfaceHolder mSurfaceHolder;
    private SurfaceHolder.Callback mSHCallback = new SurfaceHolder.Callback() {

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            mSurfaceHolder = holder;
            openVideo();
            handler.sendEmptyMessageDelayed(102, 500);
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
        }

    };
    private boolean screenLock;
    private PlayController controller;
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            int pos;
            switch (msg.what) {
                case 101:
                    break;
                case 102:
                    if (controller != null) {
//                        System.out.println("????-->" + mMediaPlayer.getCurrentPosition() + "," + mMediaPlayer.getDuration());
                        controller.currentProgress(mMediaPlayer.getCurrentPosition(), mMediaPlayer.getDuration());

                    }
                    handler.sendEmptyMessageDelayed(102, 500);
                    break;
            }
        }
    };
    private int mCurrentBufferPercentage;
    private MediaPlayer.OnBufferingUpdateListener mBufferingUpdateListener = new MediaPlayer.OnBufferingUpdateListener() {

        @Override
        public void onBufferingUpdate(MediaPlayer mp, int percent) {
            mCurrentBufferPercentage = percent;
        }

    };
    /**
     * ??????,???????
     */
    private MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mMediaPlayer.start();
            handler.sendEmptyMessageDelayed(102, 1000);
        }
    };
    /**
     * ???????
     */
    private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
        }
    };
    /**
     * ???????
     */
    private MediaPlayer.OnErrorListener mErrorListener = new MediaPlayer.OnErrorListener() {

        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            return false;
        }
    };
    private MediaPlayer mMediaPlayer;
    private Uri mUri;

    public SuperVideoView(Context context) {
        super(context);
        initVideoView();
        mContext = context;
    }

    private void initVideoView() {
        requestFocus();
        getHolder().addCallback(mSHCallback);
    }

    public SuperVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initVideoView();
        mContext = context;
    }

    public SuperVideoView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initVideoView();
        mContext = context;
    }

    /**
     * ???????????????
     *
     * @return ????
     */
    public int getCurrentProgress() {
        if (mMediaPlayer != null) {
            return mMediaPlayer.getCurrentPosition();
        } else {
            return 0;
        }
    }

    /**
     * ?????????
     *
     * @return true:??,false:????
     */
    public boolean isPlaying() {
        if (mMediaPlayer == null) {
            return false;
        } else {
            return mMediaPlayer.isPlaying();
        }
    }

    public void setVideoPath(String path) {
        setVideoURI(Uri.parse(path));
    }

    public void setVideoURI(Uri uri) {
        mUri = uri;
        openVideo();
        requestLayout();
        invalidate();
    }

    private void openVideo() {
        if (mUri == null || mSurfaceHolder == null) {
            return;
        }
        Intent i = new Intent("com.android.music.musicservicecommand");
        i.putExtra("command", "pause");
        mContext.sendBroadcast(i);
        release();
        try {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setOnPreparedListener(mPreparedListener);
            mMediaPlayer.setOnCompletionListener(mCompletionListener);
            mMediaPlayer.setOnErrorListener(mErrorListener);
            mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
            mMediaPlayer.setDataSource(mContext, mUri);
            mMediaPlayer.setDisplay(mSurfaceHolder);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setScreenOnWhilePlaying(true);
            mMediaPlayer.prepareAsync();// ??????????????
        } catch (Exception ex) {
            mMediaPlayer = null;
            ex.printStackTrace();
        }
    }

    /**
     * ??
     */
    public void release() {
        if (mMediaPlayer != null) {
            mMediaPlayer.reset();
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    /**
     * ??
     */
    public void start() {
        if (mMediaPlayer != null) {
            mMediaPlayer.start();
            handler.sendEmptyMessageDelayed(102, 500);
//            System.out.println("?????....");
        }
    }

    /**
     * ????
     */
    public void pause() {
        if (mMediaPlayer != null) {
            mMediaPlayer.pause();
        }
    }

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

    /**
     * ???????????
     */
    public void onDestroy() {
        if (handler != null) {
            handler.removeMessages(102);
            handler = null;
        }
        release();
        controller = null;
        mSurfaceHolder = null;
//        System.out.println("SuperVideoView ???");
    }

    /**
     * ????
     *
     * @param msec ??????,???
     */
    public void seekTo(int msec) {
//        if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {

        if (mMediaPlayer != null) {
            int duration = mMediaPlayer.getDuration();
            msec = msec > duration ? duration : msec;
            if(msec<0)
                msec=0;
            mMediaPlayer.seekTo(msec);
        }
    }

    /**
     * ?????????????
     *
     * @return true:???,false:????
     */
    public boolean isScreenLock() {
        return screenLock;
    }

    /**
     * ????????????
     *
     * @param screenLock true:???,false:????
     */
    public void setScreenLock(boolean screenLock) {
        this.screenLock = screenLock;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (screenLock) {
            return true;
        }
        return super.onTouchEvent(event);
    }

    public void setOnProgressListener(PlayController controller) {
        this.controller = controller;
    }

    public int getBufferPercentage() {
        if (mMediaPlayer != null) {
            return mCurrentBufferPercentage;
        }
        return 0;
    }

    public interface PlayController {
        void currentProgress(int current, int total);
    }

}




Java Source Code List

com.huilan.library_videoplay.AdvancedPlayerSimple.java
com.huilan.library_videoplay.AdvancedPlayer.java
com.huilan.library_videoplay.VideoViewPlay.java
com.huilan.library_videoplay.sample.LightnessController.java
com.huilan.library_videoplay.sample.MainActivity.java
com.huilan.library_videoplay.sample.TextActivity.java
com.huilan.library_videoplay.util.LightnessController.java
com.huilan.library_videoplay.util.ParseHtmlToUrl.java
com.huilan.library_videoplay.util.ViewController.java
com.huilan.library_videoplay.view.FullScreenVideoView.java
com.huilan.library_videoplay.view.SuperVideoView.java