Skip to content

Commit

Permalink
Addd Aspect Ratios: 1*1 - 4*3 - 16*9 - match
Browse files Browse the repository at this point in the history
  • Loading branch information
Reza Amuzadeh committed Jul 2, 2019
1 parent 602bd25 commit 2262d6a
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 48 deletions.
17 changes: 15 additions & 2 deletions app/src/main/java/com/potyvideo/andexoplayer/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class MainActivity extends AppCompatActivity {

private String TEST_URL_MP4 = "https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_5mb.mp4";

private String TEST_URL_HLS = "http://185.23.131.61/hls/jomhori_2.m3u8";

private int req_code = 129;

@Override
Expand All @@ -32,20 +34,31 @@ protected void onCreate(Bundle savedInstanceState) {
// andExoPlayerView.setShowFullScreen(true);
// andExoPlayerView.setResizeMode(EnumResizeMode.FILL);

findViewById(R.id.appCompatButtonLocale).setOnClickListener(new View.OnClickListener() {
findViewById(R.id.local).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectLocaleVideo();
}
});

findViewById(R.id.appCompatButtonServerSide).setOnClickListener(new View.OnClickListener() {
findViewById(R.id.mp4).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadMP4ServerSide();
}
});

findViewById(R.id.hls).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadHls();
}
});

}

private void loadHls() {
andExoPlayerView.setSource(TEST_URL_HLS);
}

private void loadMP4ServerSide() {
Expand Down
37 changes: 8 additions & 29 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@
android:id="@+id/andExoPlayerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:andexo_aspect_ratio="aspect_16_9"
app:andexo_full_screen="true"
app:andexo_play_when_ready="true"
app:andexo_resize_mode="Fit" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/local"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="click 1" />
android:text="PLAY LOCAL" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/mp4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="click 2" />
android:text="PLAY MP4" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/hls"
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" />
android:text="PLAY HLS" />

<TextView
android:layout_width="match_parent"
Expand All @@ -39,25 +39,4 @@
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:id="@+id/appCompatButtonServerSide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Server Side MP4" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/appCompatButtonLocale"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Play Locale" />

</LinearLayout>

</LinearLayout>
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 4
versionName "1.0.3"
versionCode 5
versionName "1.0.4"
vectorDrawables.useSupportLibrary = true

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
70 changes: 56 additions & 14 deletions library/src/main/java/com/potyvideo/library/AndExoPlayerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,22 @@
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.potyvideo.library.globalEnums.EnumAspectRatio;
import com.potyvideo.library.globalEnums.EnumResizeMode;
import com.potyvideo.library.utils.PublicFunctions;
import com.potyvideo.library.utils.PublicValues;

public class AndExoPlayerView extends LinearLayout implements View.OnClickListener {

private Context context;
private String currSource = "";
private boolean playWhenReady = false;
private int currentWindow = 0;
private long playbackPosition = 0;
private boolean isPreparing = false;
private TypedArray typedArray = null;
private boolean currPlayWhenReady = false;
private EnumResizeMode currResizeMode = EnumResizeMode.FILL;
private EnumAspectRatio currAspectRatio = EnumAspectRatio.ASPECT_16_9;

private SimpleExoPlayer simpleExoPlayer;
private PlayerView playerView;
Expand Down Expand Up @@ -186,20 +190,27 @@ private void initializeView(Context context) {

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));
}

if (typedArray.hasValue(R.styleable.AndExoPlayerView_andexo_aspect_ratio)) {
int aspectRatio = typedArray.getInteger(R.styleable.AndExoPlayerView_andexo_aspect_ratio, EnumAspectRatio.ASPECT_16_9.getValue());
setAspectRatio(EnumAspectRatio.get(aspectRatio));
}

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_play_when_ready)) {
setPlayWhenReady(typedArray.getBoolean(R.styleable.AndExoPlayerView_andexo_play_when_ready, false));
}

typedArray.recycle();
}

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

initializePlayer();
}

Expand All @@ -215,7 +226,7 @@ private void initializePlayer() {
simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(context, trackSelector);

playerView.setPlayer(simpleExoPlayer);
simpleExoPlayer.setPlayWhenReady(playWhenReady);
simpleExoPlayer.setPlayWhenReady(currPlayWhenReady);
simpleExoPlayer.seekTo(currentWindow, playbackPosition);
}
}
Expand Down Expand Up @@ -263,15 +274,17 @@ private void releasePlayer() {
if (simpleExoPlayer != null) {
playbackPosition = simpleExoPlayer.getCurrentPosition();
currentWindow = simpleExoPlayer.getCurrentWindowIndex();
playWhenReady = simpleExoPlayer.getPlayWhenReady();
currPlayWhenReady = simpleExoPlayer.getPlayWhenReady();
simpleExoPlayer.removeListener(componentListener);
simpleExoPlayer.release();
simpleExoPlayer = null;
}
}

public void setPlayWhenReady(boolean playWhenReady) {
this.playWhenReady = playWhenReady;
this.currPlayWhenReady = playWhenReady;
if (simpleExoPlayer != null)
simpleExoPlayer.setPlayWhenReady(playWhenReady);
}

public void stopPlayer() {
Expand Down Expand Up @@ -311,6 +324,36 @@ public void setShowFullScreen(boolean showFullScreen) {
frameLayoutFullScreenContainer.setVisibility(GONE);
}

public void setAspectRatio(EnumAspectRatio aspectRatio) {
this.currAspectRatio = aspectRatio;
int value = PublicFunctions.getScreenWidth();

switch (aspectRatio) {

case ASPECT_1_1:
playerView.setLayoutParams(new FrameLayout.LayoutParams(value, value));
break;

case ASPECT_4_3:
playerView.setLayoutParams(new FrameLayout.LayoutParams(value, (3 * value) / 4));
break;

case ASPECT_16_9:
playerView.setLayoutParams(new FrameLayout.LayoutParams(value, (9 * value) / 16));
break;

case ASPECT_MATCH:
playerView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
break;

case UNDEFINE:
default:
int baseHeight = (int) getResources().getDimension(R.dimen.player_base_height);
playerView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, baseHeight));
break;
}
}

private Activity getActivity() {
Context context = getContext();
while (context instanceof ContextWrapper) {
Expand All @@ -334,17 +377,16 @@ protected void onConfigurationChanged(Configuration newConfig) {

// Checking the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//First Hide other objects (listview or recyclerview), better hide them using Gone.
// First Hide other objects (listview or recyclerview), better hide them using Gone.
hideSystemUi();
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) playerView.getLayoutParams();
params.width = params.MATCH_PARENT;
params.height = params.MATCH_PARENT;
playerView.setLayoutParams(params);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//unhide your objects here.
// unhide your objects here.
showSystemUi();
int baseHeight = (int) getResources().getDimension(R.dimen.player_base_height);
playerView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, baseHeight));
setAspectRatio(currAspectRatio);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.potyvideo.library.globalEnums;

public enum EnumAspectRatio {

UNDEFINE("UNDEFINE", 0),
ASPECT_1_1("ASPECT_1_1", 1),
ASPECT_16_9("ASPECT_16_9", 2),
ASPECT_4_3("ASPECT_4_3", 3),
ASPECT_MATCH("ASPECT_MATCH", 4),
;

private String valueStr;

private Integer value;

EnumAspectRatio(String valueStr, Integer value) {
this.valueStr = valueStr;
this.value = value;
}

public static EnumAspectRatio get(String value) {
if (value == null) {
return UNDEFINE;
}

EnumAspectRatio[] arr$ = values();
for (EnumAspectRatio val : arr$) {
if (val.valueStr.equalsIgnoreCase(value.trim())) {
return val;
}
}

return UNDEFINE;
}

public static EnumAspectRatio get(Integer value) {

if (value == null) {
return UNDEFINE;
}

EnumAspectRatio[] arr$ = values();
for (EnumAspectRatio val : arr$) {
if (val.value == value) {
return val;
}
}

return UNDEFINE;
}

public String getValueStr() {
return valueStr;
}

public Integer getValue() {
return value;
}

public void setValueStr(String valueStr) {
this.valueStr = valueStr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;

Expand Down Expand Up @@ -41,4 +42,12 @@ public static boolean checkAccessStoragePermission(Activity activity) {
}
return true;
}

public static int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}

public static int getScreenHeight() {
return Resources.getSystem().getDisplayMetrics().heightPixels;
}
}
9 changes: 9 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

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

<attr name="andexo_resize_mode" format="enum">
Expand All @@ -12,4 +14,11 @@
<enum name="Zoom" value="3" />
</attr>

<attr name="andexo_aspect_ratio" format="enum">
<enum name="aspect_1_1" value="1" />
<enum name="aspect_16_9" value="2" />
<enum name="aspect_4_3" value="3" />
<enum name="aspect_match" value="4" />
</attr>

</resources>
2 changes: 1 addition & 1 deletion library/src/main/res/values/dimen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
<dimen name="global_margin_extra">18dp</dimen>
<dimen name="global_margin_x_extra">24dp</dimen>

<dimen name="player_base_height">250dp</dimen>
<dimen name="player_base_height">220dp</dimen>

</resources>

0 comments on commit 2262d6a

Please sign in to comment.