package com.application.zhangshi_app_android.ui;
|
|
import android.app.Activity;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.content.pm.ActivityInfo;
|
import android.os.Bundle;
|
import android.os.Parcel;
|
import android.os.Parcelable;
|
|
import androidx.annotation.NonNull;
|
|
import com.android.app_base.base.view.BaseActivity;
|
import com.android.app_base.widget.VideoPlayerView;
|
import com.application.zhangshi_app_android.R;
|
import com.application.zhangshi_app_android.databinding.ActivityVideoPlayBinding;
|
import com.gyf.immersionbar.BarHide;
|
import com.gyf.immersionbar.ImmersionBar;
|
|
import java.io.File;
|
|
/**
|
* @author Ljj
|
* @date 2023.08.19. 20:14
|
* @desc 视频播放 Activity
|
*/
|
public class VideoPlayActivity extends BaseActivity<ActivityVideoPlayBinding,SimpleViewModel>
|
implements VideoPlayerView.OnPlayListener {
|
|
public static final String INTENT_KEY_PARAMETERS = "parameters";
|
|
private VideoPlayActivity.Builder mBuilder;
|
|
@Override
|
public int getLayoutId() {
|
return R.layout.activity_video_play;
|
}
|
|
@Override
|
public int getVariableId() {
|
return 0;
|
}
|
|
@Override
|
public void initParam() {
|
mBuilder = getIntent().getParcelableExtra(INTENT_KEY_PARAMETERS);
|
if (mBuilder == null) {
|
throw new IllegalArgumentException("are you ok?");
|
}
|
}
|
|
@Override
|
public void initView() {
|
binding.videoplayerView.setLifecycleOwner(this);
|
binding.videoplayerView.setOnPlayListener(this);
|
|
binding.videoplayerView.setVideoTitle(mBuilder.getVideoTitle());
|
binding.videoplayerView.setVideoSource(mBuilder.getVideoSource());
|
binding.videoplayerView.setGestureEnabled(mBuilder.isGestureEnabled());
|
|
if (mBuilder.isAutoPlay()) {
|
binding.videoplayerView.start();
|
}
|
}
|
|
@Override
|
public void initData() {
|
|
}
|
|
@Override
|
public void initLiveDataObserve() {
|
|
}
|
|
|
@Override
|
public void onClickBack(VideoPlayerView view) {
|
onBackPressed();
|
}
|
|
@Override
|
public void onPlayStart(VideoPlayerView view) {
|
int progress = mBuilder.getPlayProgress();
|
if (progress > 0) {
|
binding.videoplayerView.setProgress(progress);
|
}
|
}
|
|
@Override
|
public void onPlayProgress(VideoPlayerView view) {
|
// 记录播放进度
|
mBuilder.setPlayProgress(view.getProgress());
|
}
|
|
@Override
|
public void onPlayEnd(VideoPlayerView view) {
|
if (mBuilder.isLoopPlay()) {
|
binding.videoplayerView.setProgress(0);
|
binding.videoplayerView.start();
|
return;
|
}
|
|
if (mBuilder.isAutoOver()) {
|
finish();
|
}
|
}
|
|
@NonNull
|
@Override
|
protected ImmersionBar createStatusBarConfig() {
|
return super.createStatusBarConfig()
|
// 隐藏状态栏和导航栏
|
.hideBar(BarHide.FLAG_HIDE_BAR);
|
}
|
|
@Override
|
protected void onSaveInstanceState(@NonNull Bundle outState) {
|
super.onSaveInstanceState(outState);
|
// 保存播放进度
|
outState.putParcelable(INTENT_KEY_PARAMETERS, mBuilder);
|
}
|
|
@Override
|
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
|
super.onRestoreInstanceState(savedInstanceState);
|
// 读取播放进度
|
mBuilder = savedInstanceState.getParcelable(INTENT_KEY_PARAMETERS);
|
}
|
|
/** 竖屏播放 */
|
public static final class Portrait extends VideoPlayActivity {}
|
|
/** 横屏播放 */
|
public static final class Landscape extends VideoPlayActivity {}
|
|
/**
|
* 播放参数构建
|
*/
|
public static final class Builder implements Parcelable {
|
|
/** 视频源 */
|
private String videoSource;
|
/** 视频标题 */
|
private String videoTitle;
|
|
/** 播放进度 */
|
private int playProgress;
|
/** 手势开关 */
|
private boolean gestureEnabled = true;
|
/** 循环播放 */
|
private boolean loopPlay = false;
|
/** 自动播放 */
|
private boolean autoPlay = true;
|
/** 播放完关闭 */
|
private boolean autoOver = true;
|
|
/** 播放方向 */
|
private int activityOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
|
|
public Builder() {}
|
|
protected Builder(Parcel in) {
|
videoSource = in.readString();
|
videoTitle = in.readString();
|
activityOrientation = in.readInt();
|
|
playProgress = in.readInt();
|
gestureEnabled = in.readByte() != 0;
|
loopPlay = in.readByte() != 0;
|
autoPlay = in.readByte() != 0;
|
autoOver = in.readByte() != 0;
|
}
|
|
public Builder setVideoSource(File file) {
|
videoSource = file.getPath();
|
if (videoTitle == null) {
|
videoTitle = file.getName();
|
}
|
return this;
|
}
|
|
public Builder setVideoSource(String url) {
|
videoSource = url;
|
return this;
|
}
|
|
private String getVideoSource() {
|
return videoSource;
|
}
|
|
public Builder setVideoTitle(String title) {
|
videoTitle = title;
|
return this;
|
}
|
|
private String getVideoTitle() {
|
return videoTitle;
|
}
|
|
public Builder setPlayProgress(int progress) {
|
playProgress = progress;
|
return this;
|
}
|
|
private int getPlayProgress() {
|
return playProgress;
|
}
|
|
public Builder setGestureEnabled(boolean enabled) {
|
gestureEnabled = enabled;
|
return this;
|
}
|
|
private boolean isGestureEnabled() {
|
return gestureEnabled;
|
}
|
|
public Builder setLoopPlay(boolean enabled) {
|
loopPlay = enabled;
|
return this;
|
}
|
|
private boolean isLoopPlay() {
|
return loopPlay;
|
}
|
|
public Builder setAutoPlay(boolean enabled) {
|
autoPlay = enabled;
|
return this;
|
}
|
|
public boolean isAutoPlay() {
|
return autoPlay;
|
}
|
|
public Builder setAutoOver(boolean enabled) {
|
autoOver = enabled;
|
return this;
|
}
|
|
private boolean isAutoOver() {
|
return autoOver;
|
}
|
|
public Builder setActivityOrientation(int orientation) {
|
activityOrientation = orientation;
|
return this;
|
}
|
|
public void start(Context context) {
|
Intent intent = new Intent();
|
switch (activityOrientation) {
|
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
|
intent.setClass(context, VideoPlayActivity.Landscape.class);
|
break;
|
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
|
intent.setClass(context, VideoPlayActivity.Portrait.class);
|
break;
|
default:
|
intent.setClass(context, VideoPlayActivity.class);
|
break;
|
}
|
|
intent.putExtra(INTENT_KEY_PARAMETERS, this);
|
if (!(context instanceof Activity)) {
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
}
|
context.startActivity(intent);
|
}
|
|
@Override
|
public int describeContents() {
|
return 0;
|
}
|
|
@Override
|
public void writeToParcel(Parcel dest, int flags) {
|
dest.writeString(videoSource);
|
dest.writeString(videoTitle);
|
dest.writeInt(activityOrientation);
|
dest.writeInt(playProgress);
|
dest.writeByte(gestureEnabled ? (byte) 1 : (byte) 0);
|
dest.writeByte(loopPlay ? (byte) 1 : (byte) 0);
|
dest.writeByte(autoPlay ? (byte) 1 : (byte) 0);
|
dest.writeByte(autoOver ? (byte) 1 : (byte) 0);
|
}
|
|
public static final Parcelable.Creator<Builder> CREATOR = new Parcelable.Creator<Builder>() {
|
@Override
|
public Builder createFromParcel(Parcel source) {
|
return new Builder(source);
|
}
|
|
@Override
|
public Builder[] newArray(int size) {
|
return new Builder[size];
|
}
|
};
|
}
|
}
|