Skip to content

Commit

Permalink
Add resize action
Browse files Browse the repository at this point in the history
  • Loading branch information
moneytoo committed Nov 29, 2020
1 parent a5ecf11 commit 46907b2
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
27 changes: 27 additions & 0 deletions app/src/main/java/com/brouken/player/PlayerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import com.google.android.exoplayer2.text.CaptionStyleCompat;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.MappingTrackSelector;
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
import com.google.android.exoplayer2.ui.DefaultTimeBar;
import com.google.android.exoplayer2.ui.StyledPlayerControlView;
import com.google.android.exoplayer2.util.MimeTypes;
Expand Down Expand Up @@ -80,6 +81,7 @@ public class PlayerActivity extends Activity {

private TextView titleView;
private ImageButton buttonPiP;
private ImageButton buttonAspectRatio;

private boolean restoreOrientationLock;
private boolean restorePlayState;
Expand Down Expand Up @@ -203,6 +205,26 @@ public void onClick(View view) {
Utils.setButtonEnabled(this, buttonPiP, false);
}

buttonAspectRatio = new ImageButton(this, null, 0, R.style.ExoStyledControls_Button_Bottom);
buttonAspectRatio.setImageResource(R.drawable.ic_baseline_aspect_ratio_24);
controls.addView(buttonAspectRatio, 5);
buttonAspectRatio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (playerView.getResizeMode() == AspectRatioFrameLayout.RESIZE_MODE_FIT) {
playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_ZOOM);
Utils.showText(playerView, "Crop");
} else {
// Default mode
playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIT);
Utils.showText(playerView, "Fit");
}
// Keep controller UI visible - alternative to resetHideCallbacks()
playerView.setControllerShowTimeoutMs(PlayerActivity.CONTROLLER_TIMEOUT);
}
});
Utils.setButtonEnabled(this, buttonAspectRatio, false);

int padding = getResources().getDimensionPixelOffset(R.dimen.exo_time_view_padding);
FrameLayout centerView = playerView.findViewById(R.id.exo_center_view);
titleView = new TextView(this);
Expand Down Expand Up @@ -398,6 +420,8 @@ public void onClick(DialogInterface dialogInterface, int i) {
if (haveMedia) {
playerView.setControllerShowTimeoutMs(CONTROLLER_TIMEOUT);

playerView.setResizeMode(mPrefs.resizeMode);

MediaItem.Builder mediaItemBuilder = new MediaItem.Builder()
.setUri(mPrefs.mediaUri)
.setMimeType(mPrefs.mediaType);
Expand Down Expand Up @@ -426,6 +450,8 @@ public void onClick(DialogInterface dialogInterface, int i) {
if (buttonPiP != null)
Utils.setButtonEnabled(this, buttonPiP, true);

Utils.setButtonEnabled(this, buttonAspectRatio, true);

player.setHandleAudioBecomingNoisy(true);
mediaSession.setActive(true);
} else {
Expand All @@ -451,6 +477,7 @@ private void releasePlayer() {
mPrefs.updateBrightness(mBrightnessControl.currentBrightnessLevel);
mPrefs.updateSubtitleTrack(getSelectedTrack(C.TRACK_TYPE_TEXT));
mPrefs.updateAudioTrack(getSelectedTrack(C.TRACK_TYPE_AUDIO));
mPrefs.updateResizeMode(playerView.getResizeMode());

if (player.isPlaying()) {
restorePlayState = true;
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/brouken/player/Prefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.net.Uri;
import android.preference.PreferenceManager;

import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
Expand All @@ -20,13 +22,15 @@ class Prefs {
private static final String PREF_KEY_SUBTITLE_URI = "subtitleUri";
private static final String PREF_KEY_AUDIO_TRACK = "audioTrack";
private static final String PREF_KEY_SUBTITLE_TRACK = "subtitleTrack";
private static final String PREF_KEY_RESIZE_MODE = "resizeMode";

Context mContext;
SharedPreferences mSharedPreferences;

public Uri mediaUri;
public Uri subtitleUri;
public String mediaType;
public int resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIT;

public int subtitleTrack = -1;
public int audioTrack = -1;
Expand Down Expand Up @@ -56,6 +60,8 @@ private void loadSavedPreferences() {
audioTrack = mSharedPreferences.getInt(PREF_KEY_AUDIO_TRACK, audioTrack);
if (mSharedPreferences.contains(PREF_KEY_SUBTITLE_TRACK))
subtitleTrack = mSharedPreferences.getInt(PREF_KEY_SUBTITLE_TRACK, subtitleTrack);
if (mSharedPreferences.contains(PREF_KEY_RESIZE_MODE))
resizeMode = mSharedPreferences.getInt(PREF_KEY_RESIZE_MODE, resizeMode);
}

public void updateMedia(final Uri uri, final String type) {
Expand All @@ -64,6 +70,7 @@ public void updateMedia(final Uri uri, final String type) {
updateSubtitle(null);
updateAudioTrack(-1);
updateSubtitleTrack(-1);
updateResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIT);

final SharedPreferences.Editor sharedPreferencesEditor = mSharedPreferences.edit();
if (uri == null)
Expand Down Expand Up @@ -166,4 +173,10 @@ public void updateSubtitleTrack(final int track) {
sharedPreferencesEditor.commit();
}

public void updateResizeMode(final int mode) {
this.resizeMode = mode;
final SharedPreferences.Editor sharedPreferencesEditor = mSharedPreferences.edit();
sharedPreferencesEditor.putInt(PREF_KEY_RESIZE_MODE, resizeMode);
sharedPreferencesEditor.commit();
}
}
6 changes: 6 additions & 0 deletions app/src/main/java/com/brouken/player/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,10 @@ public static void setButtonEnabled(final Context context, final ImageButton but
(float) context.getResources().getInteger(R.integer.exo_media_button_opacity_percentage_disabled) / 100
);
}

public static void showText(final CustomStyledPlayerView playerView, final String text) {
playerView.removeCallbacks(playerView.textClearRunnable);
playerView.setCustomErrorMessage(text);
playerView.postDelayed(playerView.textClearRunnable, 1200);
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_baseline_aspect_ratio_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M19,12h-2v3h-3v2h5v-5zM7,9h3L10,7L5,7v5h2L7,9zM21,3L3,3c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h18c1.1,0 2,-0.9 2,-2L23,5c0,-1.1 -0.9,-2 -2,-2zM21,19.01L3,19.01L3,4.99h18v14.02z"/>
</vector>
73 changes: 73 additions & 0 deletions app/src/main/res/layout/exo_styled_player_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2020 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<merge xmlns:android="http://schemas.android.com/apk/res/android">

<com.google.android.exoplayer2.ui.AspectRatioFrameLayout android:id="@id/exo_content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">

<!-- Video surface will be inserted as the first child of the content frame. -->

<View android:id="@id/exo_shutter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"/>

<ImageView android:id="@id/exo_artwork"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"/>

<ProgressBar android:id="@id/exo_buffering"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:layout_gravity="center"/>

<TextView android:id="@id/exo_error_message"
android:layout_width="wrap_content"
android:layout_height="@dimen/exo_error_message_height"
android:layout_gravity="center"
android:layout_marginBottom="@dimen/exo_error_message_margin_bottom"
android:gravity="center"
android:textColor="@color/exo_white"
android:textSize="@dimen/exo_error_message_text_size"
android:background="@drawable/exo_rounded_rectangle"
android:paddingLeft="@dimen/exo_error_message_text_padding_horizontal"
android:paddingRight="@dimen/exo_error_message_text_padding_horizontal"
android:paddingTop="@dimen/exo_error_message_text_padding_vertical"
android:paddingBottom="@dimen/exo_error_message_text_padding_vertical"/>

</com.google.android.exoplayer2.ui.AspectRatioFrameLayout>

<com.google.android.exoplayer2.ui.SubtitleView android:id="@id/exo_subtitles"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<FrameLayout android:id="@id/exo_ad_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<FrameLayout android:id="@id/exo_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<View android:id="@id/exo_controller_placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</merge>

0 comments on commit 46907b2

Please sign in to comment.