Introduction to the

This project was created because of the need for video playback in the project recently. The video codec function is handled by Bilibili’s open source IJkPlayer, which is based on the uI-level encapsulation only.

Note: some of the code for this project references GSYVideoPlayer.

update

Support for more video formats with repackaged SO files.

Thank you

  • ijkplayer
  • AndroidVideoCache
  • GSYVideoPlayer
  • ENViews

The project address

function

  • Player basics (play, pause, fast forward, etc.)
  • Support list play, automatically release the last player
  • Video cover image Settings
  • Provide two full screen videos (set screen rotation direction and add a full screen player)
  • Gesture swiping changes playback progress, screen brightness and volume
  • Simple Wifi network check
  • Video caching using AndroidVideoCache
  • Add full screen Player Transition animation (New)
  • To be continued…

usage

To use in the list, the code here is based on RecyclerView:

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View contentView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video, parent, false);
            ViewHolder viewHolder = new ViewHolder(contentView);
            viewHolder.controller = new MediaController(parent.getContext());
            viewHolder.controller.setEnableSlideBrightness(false);
            viewHolder.controller.setEnableSliderVolume(false);
            viewHolder.controller.setEnableSlidePosition(false);
// viewHolder.controller.setFullScreenViewEnableSlideBrightness(false);
// viewHolder.controller.setFullScreenViewEnableSlidePosition(false);
// viewHolder.controller.setFullScreenViewEnableSliderVolume(false);
            viewHolder.controller.setFullScreenMode(MediaController.FULLSCREEN_VIEW);
            viewHolder.controller.setMute(true);
            viewHolder.controller.setShowBottomLayout(false);
            viewHolder.video.setMediaController(viewHolder.controller);
            mVideoWidth = parent.getContext().getResources().getDisplayMetrics().widthPixels;
            mVideoHeight = viewHolder.video.getLayoutParams().height;
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, int position) {
            holder.video.setVideoPath(VIDEO_URL);
            holder.video.setPlayPosition(position);
            holder.controller.setPlayPosition(position);
            holder.controller.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) { holder.controller.toggleFullScreenView(); }});if(holder.controller.getImgThumb() ! =null) {
                Glide
                        .with(holder.itemView.getContext())
                        .load(IMGS[position])
                        .apply(RequestOptions.centerCropTransform())
                        .into(holder.controller.getImgThumb());
            } else {
                Log.d(getClass().getSimpleName(), "Thumb ImgView is null"); }}Copy the code

The important thing to note here is that using the player in a list requires the developer to manually release the player when the Item is not visible to save memory.

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if(newState == RecyclerView.SCROLL_STATE_IDLE) { autoPlay(); }}@Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                if (IjkVideoManager.getInstance().getPlayPosition() > -1) {
                    int position = IjkVideoManager.getInstance().getPlayPosition();
                    if(! isVisible(position)) { IjkVideoManager.getInstance().release(); }}}});Copy the code

It is relatively simple to use on a single player page:

        mIjkVideoView = (IjkVideoView) findViewById(R.id.video_view);
        MediaController mediaController = new MediaController(this);
        mediaController.setShowThumb(true);
        mIjkVideoView.setMediaController(mediaController);

        mIjkVideoView.setVideoPath("http://baobab.wdjcdn.com/14564977406580.mp4");Copy the code

Note that the release player needs to be called manually when the page is closed, whether used in a list or as a single player:

    @Override
    protected void onDestroy(a) {
        super.onDestroy();
        IjkVideoManager.getInstance().release();
    }

    @Override
    protected void onPause(a) {
        super.onPause();
        IjkVideoManager.getInstance().pause();
    }Copy the code

See the project for more code.

Friends who have this need can freely copy my code, the only hope is to give me some bugs, or have a better implementation, thank you.