Categories: Uncategorized

How to create video player like YouTube in android studio Part-3

Hi everyone. Today i will show you how to create a video player similar to YouTube video player having all the similar features using Android Studio.

This Blog is in continuation of previous blog

custom video player in android example

You will need to have basic knowledge of android to understand this tutorial.

In my previous blog we talked about java coding.

 

In this i will show how to create youtube layout (miniplayer like youtube)

 

create a new java file YoutubeLayout

 

package com.example.testapp;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import androidx.core.view.ViewCompat;
import androidx.customview.widget.ViewDragHelper;


public class YoutubeLayout extends ViewGroup {

   private final ViewDragHelper mDragHelper;
   private View mHeaderView;
    private View mDescView;
   private int mDragRange;
    private int mTop;
   private float mDragOffset;


    public YoutubeLayout(Context context) {
      this(context, null);
   }

   public YoutubeLayout(Context context, AttributeSet attrs) {
      this(context, attrs, 0);
   }

   public YoutubeLayout(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      mDragHelper = ViewDragHelper.create(this, 2f, new DragHelperCallback());
   }

    @Override
    protected void onFinishInflate() {
      super.onFinishInflate();
      mHeaderView = findViewById(R.id.video_layout);
      mDescView = findViewById(R.id.desc);
   }

    public void maximize() {
        smoothSlideTo(0f);
    }

    public void minimize() {
        smoothSlideTo(1f);
    }

    boolean smoothSlideTo(float slideOffset) {
        final int topBound = getPaddingTop();
        int y = (int) (topBound + slideOffset * mDragRange);

        if (mDragHelper.smoothSlideViewTo(mHeaderView, mHeaderView.getLeft(), y)) {
            ViewCompat.postInvalidateOnAnimation(this);
            return true;
        }
        return false;
    }

    private class DragHelperCallback extends ViewDragHelper.Callback {

      @Override
      public boolean tryCaptureView(View child, int pointerId) {
            return child == mHeaderView;
      }

        @Override
      public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
         mTop = top;

         mDragOffset = (float) top / mDragRange;

            mHeaderView.setPivotX(mHeaderView.getWidth());
            mHeaderView.setPivotY(mHeaderView.getHeight());
            mHeaderView.setScaleX(1 - mDragOffset / 2);
            mHeaderView.setScaleY(1 - mDragOffset / 2);

            mDescView.setAlpha(1 - mDragOffset);

            requestLayout();
      }

      @Override
      public void onViewReleased(View releasedChild, float xvel, float yvel) {
         int top = getPaddingTop();
         if (yvel > 0 || (yvel == 0 && mDragOffset > 0.5f)) {
            top += mDragRange;
         }
         mDragHelper.settleCapturedViewAt(releasedChild.getLeft(), top);
         invalidate();
      }

      @Override
      public int getViewVerticalDragRange(View child) {
         return mDragRange;
      }

      @Override
      public int clampViewPositionVertical(View child, int top, int dy) {
         final int topBound = getPaddingTop();
         final int bottomBound = getHeight() - mHeaderView.getHeight() - mHeaderView.getPaddingBottom();

         final int newTop = Math.min(Math.max(top, topBound), bottomBound);
         return newTop;
      }

   }

   @Override
   public void computeScroll() {
      if (mDragHelper.continueSettling(true)) {
         ViewCompat.postInvalidateOnAnimation(this);
      }
   }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
        int maxHeight = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, 0),
                resolveSizeAndState(maxHeight, heightMeasureSpec, 0));
    }

    @Override
   protected void onLayout(boolean changed, int l, int t, int r, int b) {
      mDragRange = getHeight() - mHeaderView.getHeight();
        mHeaderView.layout(0, mTop, r, mTop + mHeaderView.getMeasuredHeight());
        mDescView.layout(0, mTop + mHeaderView.getMeasuredHeight(), r, mTop  + b);
   }
}

 

Now add a variable to your Main Activity

private YoutubeLayout youtubeLayout;

youtubeLayout = findViewById(R.id.dragLayout);



Now call these methods to minimise and maximise the player


public void showDown(View view) {
    minimiseView();
}

public void showUp(View view) {
    maximiseView();
}

void minimiseView()
{
    showimgUp.setVisibility(View.VISIBLE);
    showimgDown.setVisibility(View.GONE);
    youtubeLayout.minimize();
    fullscreen.setVisibility(View.GONE);
    youtubeLayout.minimize();
    isMaximise=false;
}

void maximiseView()
{
    showimgUp.setVisibility(View.GONE);
    showimgDown.setVisibility(View.VISIBLE);
    youtubeLayout.maximize();
    fullscreen.setVisibility(View.VISIBLE);
    youtubeLayout.maximize();
    isMaximise=true;
}

Thats it you have successfully created a video player like youtube.

 

Recent Posts

Mooze Tech CEO & Founder, Arbind Kumar, Is Building Futuristic Products to Convert Dreams Into Realities: GoodFirms

Incorporated in 2018 and based in New Delhi, Mooze Tech believes in engineering products that…

3 years ago

Family owned Business | Pros and Cons | MoozeTech

Having a family besides you, is always advantageous. Most people think that a family owned…

4 years ago

Business after Lockdown | Best ways to kick start | MoozeTech

Business after Lockdown Like so many others, may your business have also been affected due…

4 years ago

Traditional Marketing vs Digital Marketing : Find which one is better?

Traditional Marketing vs Digital Marketing are the two main strategies to capture the attention of…

4 years ago

KNOWLEDGE REPRESENTATION IN ARTIFICIAL INTELLIGENCE

What is Knowledge Representation:-  Humans are very good at understanding and reasoning. Humans know which…

4 years ago

MOBILE COMPUTING | INTRODUCTION | MOBILE TECHNOLOGIES

Mobile computing is a set of advanced IT technologies and products. It enables users to…

4 years ago

This website uses cookies.