Skip to content

Commit

Permalink
Added FullScreen Support ;)
Browse files Browse the repository at this point in the history
  • Loading branch information
Reza Amuzadeh committed Jun 29, 2019
1 parent 581307e commit dbbe631
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 42 deletions.
7 changes: 7 additions & 0 deletions .idea/dictionaries/Reza_Amozadeh.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ protected void onCreate(Bundle savedInstanceState) {

andExoPlayerView = findViewById(R.id.andExoPlayerView);
andExoPlayerView.setSource(Uri.parse(TEST_URL_MP4));

// andExoPlayerView.setShowFullScreen(true);
// andExoPlayerView.setResizeMode(EnumResizeMode.FILL);
}
}
55 changes: 51 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
android:orientation="vertical">

<com.potyvideo.library.AndExoPlayerView
android:id="@+id/andExoPlayerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
app:andexo_full_screen="true"
app:andexo_resize_mode="Fill" />

<androidx.appcompat.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="click 1" />

<androidx.appcompat.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="click 2" />

<androidx.appcompat.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="click 3" />

<androidx.appcompat.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="click 4" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="@dimen/global_padding_extra"
android:text="SOME OTHER VIEWS" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<androidx.appcompat.widget.AppCompatButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="click 5" />

<androidx.appcompat.widget.AppCompatButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="click 6" />

</LinearLayout>

</LinearLayout>
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
6 changes: 4 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
group = 'com.github.HamidrezaAmz'

android {

Expand All @@ -8,8 +10,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.0.1"
vectorDrawables.useSupportLibrary = true

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
113 changes: 82 additions & 31 deletions library/src/main/java/com/potyvideo/library/AndExoPlayerView.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package com.potyvideo.library;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageButton;

import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.PlaybackParameters;
Expand All @@ -37,31 +42,31 @@
import com.google.android.exoplayer2.upstream.BandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.potyvideo.library.globalEnums.EnumPlayerSize;
import com.potyvideo.library.globalEnums.EnumResizeMode;
import com.potyvideo.library.utils.PublicValues;

public class AndExoPlayerView extends LinearLayout {
public class AndExoPlayerView extends LinearLayout implements View.OnClickListener {

private Context context;
private Uri currUri = null;
private boolean playWhenReady = false;
private int currentWindow = 0;
private long playbackPosition = 0;
private boolean isPreparing = false;
private TypedArray typedArray = null;

private SimpleExoPlayer simpleExoPlayer;
private PlayerView playerView;
private ComponentListener componentListener;
private LinearLayout linearLayoutRetry;
private FrameLayout frameLayoutFullScreenContainer;
private AppCompatImageButton imageViewEnterFullScreen, imageViewExitFullScreen;

private BandwidthMeter bandwidthMeter;
private ExtractorsFactory extractorsFactory;
private TrackSelection.Factory trackSelectionFactory;
private TrackSelector trackSelector;

private EnumPlayerSize currPlayerSize = EnumPlayerSize.AT_MOST;

public class ComponentListener implements Player.EventListener {

String TAG = AndExoPlayerView.ComponentListener.class.getSimpleName();
Expand Down Expand Up @@ -142,11 +147,19 @@ public AndExoPlayerView(Context context) {

public AndExoPlayerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
typedArray = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.AndExoPlayerView,
0, 0);
initializeView(context);
}

public AndExoPlayerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
typedArray = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.AndExoPlayerView,
0, 0);
initializeView(context);
}

Expand All @@ -158,9 +171,32 @@ private void initializeView(Context context) {

playerView = view.findViewById(R.id.simpleExoPlayerView);
linearLayoutRetry = findViewById(R.id.linearLayoutRetry);
frameLayoutFullScreenContainer = playerView.findViewById(R.id.container_fullscreen);
imageViewEnterFullScreen = playerView.findViewById(R.id.exo_enter_fullscreen);
imageViewExitFullScreen = playerView.findViewById(R.id.exo_exit_fullscreen);

componentListener = new ComponentListener();

linearLayoutRetry.setOnClickListener(this);
imageViewEnterFullScreen.setOnClickListener(this);
imageViewExitFullScreen.setOnClickListener(this);

if (typedArray != null) {

if (typedArray.hasValue(R.styleable.AndExoPlayerView_andexo_full_screen))
setShowFullScreen(typedArray.getBoolean(R.styleable.AndExoPlayerView_andexo_full_screen, false));

if (typedArray.hasValue(R.styleable.AndExoPlayerView_andexo_resize_mode)) {
int resizeMode = typedArray.getInteger(R.styleable.AndExoPlayerView_andexo_resize_mode, EnumResizeMode.FILL.getValue());
setResizeMode(EnumResizeMode.get(resizeMode));
}

typedArray.recycle();
}

int baseHeight = (int) getResources().getDimension(R.dimen.player_base_height);
playerView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, baseHeight));

initializePlayer();
}

Expand Down Expand Up @@ -231,11 +267,6 @@ public void hideController() {
playerView.hideController();
}

public void fullScreen() {
playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL);
simpleExoPlayer.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
}

public void setResizeMode(EnumResizeMode resizeMode) {
switch (resizeMode) {

Expand All @@ -256,8 +287,22 @@ public void setResizeMode(EnumResizeMode resizeMode) {
}
}

public void setPlayerSize(EnumPlayerSize playerSize) {
this.currPlayerSize = playerSize;
public void setShowFullScreen(boolean showFullScreen) {
if (showFullScreen)
frameLayoutFullScreenContainer.setVisibility(VISIBLE);
else
frameLayoutFullScreenContainer.setVisibility(GONE);
}

private Activity getActivity() {
Context context = getContext();
while (context instanceof ContextWrapper) {
if (context instanceof Activity) {
return (Activity) context;
}
context = ((ContextWrapper) context).getBaseContext();
}
return null;
}

@Override
Expand All @@ -281,10 +326,8 @@ protected void onConfigurationChanged(Configuration newConfig) {
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//unhide your objects here.
showSystemUi();
/*FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) playerView.getLayoutParams();
params.width = params.MATCH_PARENT;
params.height = PublicFunctions.convertPixelsToDp(context, PublicValues.PLAYER_HEIGHT);
playerView.setLayoutParams(params);*/
int baseHeight = (int) getResources().getDimension(R.dimen.player_base_height);
playerView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, baseHeight));
}
}

Expand All @@ -311,24 +354,32 @@ private void showSystemUi() {
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
switch (currPlayerSize) {
public void onClick(View v) {

int targetViewId = v.getId();
if (targetViewId == R.id.linearLayoutRetry) {
initializeView(context);
} else if (targetViewId == R.id.exo_enter_fullscreen) {
enterFullScreen();
} else if (targetViewId == R.id.exo_exit_fullscreen) {
exitFullScreen();
}
}

case EXACTLY:
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
break;
private void enterFullScreen() {
imageViewExitFullScreen.setVisibility(VISIBLE);
imageViewEnterFullScreen.setVisibility(GONE);

case AT_MOST:
heightMeasureSpec = (widthMeasureSpec / 3) * 2;
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
break;
if (getActivity() != null)
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

case UNSPECIFIED:
default:
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
break;
}
private void exitFullScreen() {
imageViewExitFullScreen.setVisibility(GONE);
imageViewEnterFullScreen.setVisibility(VISIBLE);

if (getActivity() != null)
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

}
2 changes: 1 addition & 1 deletion library/src/main/res/layout/layout_player_base.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<com.google.android.exoplayer2.ui.SimpleExoPlayerView
Expand Down
8 changes: 5 additions & 3 deletions library/src/main/res/layout/video_player_exo_controllers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@

</RelativeLayout>

<RelativeLayout
<FrameLayout
android:id="@+id/container_fullscreen"
android:layout_width="40dp"
android:layout_height="40dp">
android:layout_height="40dp"
android:visibility="gone">

<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/exo_enter_fullscreen"
Expand All @@ -93,8 +94,9 @@
android:layout_height="match_parent"
android:background="@color/blackTransparent"
android:tint="@color/white"
android:visibility="gone"
app:srcCompat="@drawable/ic_fullscreen_exit_black_24dp" />

</RelativeLayout>
</FrameLayout>

</LinearLayout>
15 changes: 15 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="AndExoPlayerView">
<attr name="andexo_resize_mode" />
<attr name="andexo_full_screen" format="boolean" />
</declare-styleable>

<attr name="andexo_resize_mode" format="enum">
<enum name="Fit" value="1" />
<enum name="Fill" value="2" />
<enum name="Zoom" value="3" />
</attr>

</resources>
4 changes: 3 additions & 1 deletion library/src/main/res/values/dimen.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<resources>

<dimen name="global_padding_small">6dp</dimen>
<dimen name="global_padding">8dp</dimen>
Expand All @@ -15,4 +15,6 @@
<dimen name="global_margin_extra">18dp</dimen>
<dimen name="global_margin_x_extra">24dp</dimen>

<dimen name="player_base_height">200dp</dimen>

</resources>

0 comments on commit dbbe631

Please sign in to comment.