diff --git a/app/src/main/java/com/potyvideo/andexoplayer/MainActivity.java b/app/src/main/java/com/potyvideo/andexoplayer/MainActivity.java index 77dcc09..730a39f 100644 --- a/app/src/main/java/com/potyvideo/andexoplayer/MainActivity.java +++ b/app/src/main/java/com/potyvideo/andexoplayer/MainActivity.java @@ -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 @@ -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() { diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 790a367..236921c 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -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" /> + android:text="PLAY LOCAL" /> + android:text="PLAY MP4" /> - - + android:text="PLAY HLS" /> - - - - - - - - \ No newline at end of file diff --git a/library/build.gradle b/library/build.gradle index 7930368..a0f7119 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -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" diff --git a/library/src/main/java/com/potyvideo/library/AndExoPlayerView.java b/library/src/main/java/com/potyvideo/library/AndExoPlayerView.java index d775b72..82b0c7d 100644 --- a/library/src/main/java/com/potyvideo/library/AndExoPlayerView.java +++ b/library/src/main/java/com/potyvideo/library/AndExoPlayerView.java @@ -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; @@ -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(); } @@ -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); } } @@ -263,7 +274,7 @@ 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; @@ -271,7 +282,9 @@ private void releasePlayer() { } public void setPlayWhenReady(boolean playWhenReady) { - this.playWhenReady = playWhenReady; + this.currPlayWhenReady = playWhenReady; + if (simpleExoPlayer != null) + simpleExoPlayer.setPlayWhenReady(playWhenReady); } public void stopPlayer() { @@ -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) { @@ -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); } } diff --git a/library/src/main/java/com/potyvideo/library/globalEnums/EnumAspectRatio.java b/library/src/main/java/com/potyvideo/library/globalEnums/EnumAspectRatio.java new file mode 100644 index 0000000..88b5c0d --- /dev/null +++ b/library/src/main/java/com/potyvideo/library/globalEnums/EnumAspectRatio.java @@ -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; + } +} diff --git a/library/src/main/java/com/potyvideo/library/utils/PublicFunctions.java b/library/src/main/java/com/potyvideo/library/utils/PublicFunctions.java index 51312a4..f93e8aa 100644 --- a/library/src/main/java/com/potyvideo/library/utils/PublicFunctions.java +++ b/library/src/main/java/com/potyvideo/library/utils/PublicFunctions.java @@ -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; @@ -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; + } } diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 7da03c6..16849c5 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -3,7 +3,9 @@ + + @@ -12,4 +14,11 @@ + + + + + + + \ No newline at end of file diff --git a/library/src/main/res/values/dimen.xml b/library/src/main/res/values/dimen.xml index 4cd2f23..6841786 100644 --- a/library/src/main/res/values/dimen.xml +++ b/library/src/main/res/values/dimen.xml @@ -15,6 +15,6 @@ 18dp 24dp - 250dp + 220dp \ No newline at end of file