Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support video scale and different playback speed #94

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
*/
public class SelectionActivity extends Activity {

private static final String VIDEO_URL = "https://tungsten.aaplimg.com/VOD/bipbop_adv_fmp4_example/master.m3u8";
// private static final String VIDEO_URL = "https://tungsten.aaplimg.com/VOD/bipbop_adv_fmp4_example/master.m3u8";
private static final String VIDEO_URL = "http://frqj.adrise.tv/3780244b-a47c-4c93-9455-cdd43403a553/icra2sak05.m3u8?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwbGF0Zm9ybSI6ImFuZHJvaWQiLCJleHAiOjE1NDU4MjU2MDB9.llMFV_QlfC5rixXbVNdDgWtnMXBZXJYjbh8gVHcSABk";

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.tubitv.demo.R;
import com.tubitv.demo.databinding.ExampleUiControlBinding;
import com.tubitv.media.bindings.UserController;
import com.tubitv.media.demo.enums.ScaleMode;
import com.tubitv.media.demo.presenters.ScalePresenter;
import com.tubitv.media.utilities.ExoPlayerLogger;

public class PlayerControllerUI extends FrameLayout implements View.OnClickListener {
Expand All @@ -22,6 +24,8 @@ public class PlayerControllerUI extends FrameLayout implements View.OnClickListe

ExampleUiControlBinding binding;

private ScalePresenter mScalePresenter;

public PlayerControllerUI(final Context context) {
this(context, null);
}
Expand All @@ -38,6 +42,8 @@ public PlayerControllerUI(final Context context, @Nullable final AttributeSet at
public View setController(UserController controller) {
this.mUserController = controller;
binding.setController(mUserController);
mScalePresenter = new ScalePresenter(getContext(), mUserController);
binding.videoScaleButton.setText(mScalePresenter.getCurrentScaleMode().getDescription());
return this;
}

Expand Down Expand Up @@ -69,6 +75,18 @@ public View setController(UserController controller) {
printVideoDetail();
break;

case "scale":
mScalePresenter.doScale();
ScaleMode scaleMode = mScalePresenter.getCurrentScaleMode();
binding.videoScaleButton.setText(scaleMode.getDescription());
break;

case "speedx2":
mUserController.setPlaybackSpeed(2.0f);
break;
case "speedx4":
mUserController.setPlaybackSpeed(4.0f);
break;
default:
return;
}
Expand All @@ -82,7 +100,9 @@ private void initLayout(Context context) {
binding.getRoot().findViewById(R.id.rewind).setOnClickListener(this);
binding.getRoot().findViewById(R.id.play_pause).setOnClickListener(this);
binding.getRoot().findViewById(R.id.fastford).setOnClickListener(this);

binding.videoScaleButton.setOnClickListener(this);
binding.videoSpeedX2Button.setOnClickListener(this);
binding.videoSpeedX4Button.setOnClickListener(this);
}

private void printVideoDetail() {
Expand All @@ -96,6 +116,7 @@ private void printVideoDetail() {
builder.append(mUserController.currentProgressPosition());

ExoPlayerLogger.i(TAG, builder.toString());

}

}
42 changes: 42 additions & 0 deletions app/src/main/java/com/tubitv/media/demo/enums/ScaleMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.tubitv.media.demo.enums;

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

public enum ScaleMode {
MODE_DEFAULT(RESIZE_MODE_FIT, -1, "default"),
MODE_4_3(RESIZE_MODE_FIT, -1, "4:3"),
MODE_16_9(RESIZE_MODE_FIT, -1, "16:9"),
MODE_FULL_SCREEN(RESIZE_MODE_FILL, -1, "full screen"),;

private int mMode;
private int mResId;
private String mDescription;

ScaleMode(int mode, int restId, String description) {
mMode = mode;
mResId = restId;
mDescription = description;
}

public int getMode() {
return mMode;
}

public int getRestId() {
return mResId;
}

public String getDescription() {
return mDescription;
}

public ScaleMode nextMode() {
ScaleMode[] modes = ScaleMode.values();
for (int i = 0; i < modes.length; i++) {
if (this == modes[i]) {
return modes[(++i) % modes.length];
}
}
return MODE_DEFAULT;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.tubitv.media.demo.presenters;

import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.view.Display;
import android.view.WindowManager;
import com.tubitv.media.bindings.UserController;
import com.tubitv.media.demo.enums.ScaleMode;

public class ScalePresenter {

private Context mContext;
private UserController mUserController;
private ScaleMode mCurrentScaleMode = ScaleMode.MODE_DEFAULT;

public ScalePresenter(Context context, UserController userController) {
mContext = context;
mUserController = userController;
doScale(mCurrentScaleMode);
}

public void doScale() {
ScaleMode nextScaleMode = mCurrentScaleMode.nextMode();
doScale(nextScaleMode);
mCurrentScaleMode = nextScaleMode;
}

private void doScale(ScaleMode mode) {
switch (mode) {
case MODE_DEFAULT:
float initVideoAspectRatio = mUserController.getInitVideoAspectRatio();
if (initVideoAspectRatio > 0) {
mUserController.setVideoAspectRatio(initVideoAspectRatio);
}
break;
case MODE_4_3:
mUserController.setVideoAspectRatio((float) 4 / 3);
break;
case MODE_16_9:
mUserController.setVideoAspectRatio((float) 16 / 9);
break;
case MODE_FULL_SCREEN:
mUserController.setVideoAspectRatio(getScreenWidthHeightRatio());
break;
}
mUserController.setResizeMode(mode.getMode());
}

public ScaleMode getCurrentScaleMode() {
return mCurrentScaleMode;
}

private float getScreenWidthHeightRatio() {
WindowManager windowManager =
(WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
final Display display = windowManager.getDefaultDisplay();
Point outPoint = new Point();
if (Build.VERSION.SDK_INT >= 19) {
// include navigation bar
display.getRealSize(outPoint);
} else {
// exclude navigation bar
display.getSize(outPoint);
}

return (float) outPoint.x / outPoint.y;
}
}
35 changes: 35 additions & 0 deletions app/src/main/res/layout/example_ui_control.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,41 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/video_scale_button"
android:layout_width="100dp"
android:layout_height="40dp"
android:background="@color/colorAccent"
android:clickable="true"
android:gravity="center"
android:text="Origin"
android:tag="scale"
android:textColor="@color/tubi_tv_player_white" />

<TextView
android:id="@+id/video_speed_x2_button"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginLeft="150dp"
android:background="@color/colorAccent"
android:clickable="true"
android:gravity="center"
android:text="speed X2"
android:tag="speedx2"
android:textColor="@color/tubi_tv_player_white" />

<TextView
android:id="@+id/video_speed_x4_button"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginLeft="300dp"
android:background="@color/colorAccent"
android:clickable="true"
android:gravity="center"
android:text="speed X4"
android:tag="speedx4"
android:textColor="@color/tubi_tv_player_white" />

<Button
android:id="@+id/rewind"
android:layout_width="100dp"
Expand Down
40 changes: 40 additions & 0 deletions lib/src/main/java/com/tubitv/media/bindings/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.trackselection.TrackSelectionArray;
import com.google.android.exoplayer2.video.VideoListener;
import com.tubitv.media.R;
import com.tubitv.media.interfaces.PlaybackActionCallback;
import com.tubitv.media.interfaces.TubiPlaybackControlInterface;
Expand Down Expand Up @@ -113,6 +114,21 @@ public class UserController extends BaseObservable

private int mControlState = NORMAL_CONTROL_STATE;

private float mInitVideoAspectRatio;

private VideoListener mVideoListener = new VideoListener() {
@Override
public void onVideoSizeChanged(final int width, final int height, final int unappliedRotationDegrees,
final float pixelWidthHeightRatio) {
ExoPlayerLogger.d(TAG, "onVideoSizeChanged");
mInitVideoAspectRatio = height == 0 ? 1 : (width * pixelWidthHeightRatio) / height;
}

@Override public void onRenderedFirstFrame() {
ExoPlayerLogger.d(TAG, "onRenderedFirstFrame");
}
};

/**
* Every time the {@link com.tubitv.media.fsm.state_machine.FsmPlayer} change states between
* {@link com.tubitv.media.fsm.concrete.AdPlayingState} and {@link com.tubitv.media.fsm.concrete.MoviePlayingState},
Expand Down Expand Up @@ -189,6 +205,7 @@ public void setPlayer(@NonNull SimpleExoPlayer player, @NonNull PlaybackActionCa
this.mPlayer = player;

mPlayer.addListener(this);
mPlayer.addVideoListener(mVideoListener);
playerPlaybackState.set(mPlayer.getPlaybackState());
mPlaybackActionCallback = playbackActionCallback;
updateProgress();
Expand Down Expand Up @@ -445,6 +462,29 @@ public void onStopTrackingTouch(final SeekBar seekBar) {
ExoPlayerLogger.i(TAG, "onStopTrackingTouch");
}

@Override public void setVideoAspectRatio(float widthHeightRatio) {
if (mTubiExoPlayerView != null) {
mTubiExoPlayerView.setAspectRatio(widthHeightRatio);
}
ExoPlayerLogger.i(TAG, "setVideoAspectRatio " + widthHeightRatio);

}

@Override public float getInitVideoAspectRatio() {
ExoPlayerLogger.i(TAG, "getInitVideoAspectRatio " + mInitVideoAspectRatio);
return mInitVideoAspectRatio;
}

@Override public void setResizeMode(final int resizeMode) {
if (mTubiExoPlayerView != null) {
mTubiExoPlayerView.setResizeMode(resizeMode);
}
}

@Override public void setPlaybackSpeed(final float speed) {
mPlayer.setPlaybackParameters(new PlaybackParameters(speed));
}

//---------------------------------------private method---------------------------------------------------------------------------//

private void setPlaybackState() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.tubitv.media.interfaces;

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

/**
* Created by stoyan tubi_tv_quality_on 4/27/17.
*/
Expand Down Expand Up @@ -29,4 +31,11 @@ public interface TubiPlaybackControlInterface {

long currentBufferPosition();

void setVideoAspectRatio(float widthHeightRatio);

float getInitVideoAspectRatio();

void setResizeMode(@AspectRatioFrameLayout.ResizeMode int resizeMode);

void setPlaybackSpeed(float speed);
}
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public void setPlayer(SimpleExoPlayer player, @NonNull PlaybackActionCallback pl
} else if (surfaceView instanceof SurfaceView) {
player.setVideoSurfaceView((SurfaceView) surfaceView);
}
player.setVideoListener(componentListener);
player.addVideoListener(componentListener);
player.setTextOutput(componentListener);
player.addListener(componentListener);
}
Expand All @@ -264,6 +264,10 @@ public void setResizeMode(@AspectRatioFrameLayout.ResizeMode int resizeMode) {
contentFrame.setResizeMode(resizeMode);
}

public void setAspectRatio(float widthHeightRatio){
contentFrame.setAspectRatio(widthHeightRatio);
}

/**
* Gets the {@link SubtitleView}.
*
Expand Down