优化:替换社区视频播放器
优化:适配Android14图片权限
This commit is contained in:
@@ -10,11 +10,19 @@ import android.view.ViewGroup;
|
||||
import android.view.animation.AccelerateInterpolator;
|
||||
import android.widget.RelativeLayout;
|
||||
|
||||
import com.tencent.rtmp.ITXVodPlayListener;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.blankj.utilcode.util.LogUtils;
|
||||
import com.google.android.exoplayer2.C;
|
||||
import com.google.android.exoplayer2.DefaultLoadControl;
|
||||
import com.google.android.exoplayer2.ExoPlayer;
|
||||
import com.google.android.exoplayer2.MediaItem;
|
||||
import com.google.android.exoplayer2.PlaybackException;
|
||||
import com.google.android.exoplayer2.Player;
|
||||
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
|
||||
import com.google.android.exoplayer2.ui.StyledPlayerView;
|
||||
import com.google.android.exoplayer2.video.VideoSize;
|
||||
import com.tencent.rtmp.TXLiveConstants;
|
||||
import com.tencent.rtmp.TXVodPlayConfig;
|
||||
import com.tencent.rtmp.TXVodPlayer;
|
||||
import com.tencent.rtmp.ui.TXCloudVideoView;
|
||||
import com.yunbao.common.utils.L;
|
||||
import com.yunbao.common.views.AbsViewHolder;
|
||||
import com.yunbao.video.R;
|
||||
@@ -27,11 +35,11 @@ import com.yunbao.video.http.VideoHttpUtil;
|
||||
* 视频播放器
|
||||
*/
|
||||
|
||||
public class VideoPlayViewHolder extends AbsViewHolder implements ITXVodPlayListener, View.OnClickListener {
|
||||
public class VideoPlayViewHolder extends AbsViewHolder implements View.OnClickListener {
|
||||
|
||||
private TXCloudVideoView mTXCloudVideoView;
|
||||
private StyledPlayerView mTXCloudVideoView;
|
||||
private View mVideoCover;
|
||||
private TXVodPlayer mPlayer;
|
||||
private ExoPlayer mPlayer;
|
||||
private boolean mPaused;//生命周期暂停
|
||||
private boolean mClickPaused;//点击暂停
|
||||
private ActionListener mActionListener;
|
||||
@@ -41,7 +49,6 @@ public class VideoPlayViewHolder extends AbsViewHolder implements ITXVodPlayList
|
||||
private boolean mEndPlay;
|
||||
private VideoBean mVideoBean;
|
||||
private String mCachePath;
|
||||
private TXVodPlayConfig mTXVodPlayConfig;
|
||||
|
||||
public VideoPlayViewHolder(Context context, ViewGroup parentView) {
|
||||
super(context, parentView);
|
||||
@@ -55,17 +62,21 @@ public class VideoPlayViewHolder extends AbsViewHolder implements ITXVodPlayList
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
DefaultLoadControl control = new DefaultLoadControl.Builder()
|
||||
.setPrioritizeTimeOverSizeThresholds(false)
|
||||
.setBackBuffer(10_000, true)
|
||||
.setBufferDurationsMs(500,
|
||||
5_000,
|
||||
150,
|
||||
200)
|
||||
.build();
|
||||
|
||||
mCachePath = mContext.getCacheDir().getAbsolutePath();
|
||||
mTXCloudVideoView = (TXCloudVideoView) findViewById(R.id.video_view);
|
||||
mTXCloudVideoView.setRenderMode(TXLiveConstants.RENDER_MODE_FULL_FILL_SCREEN);
|
||||
mPlayer = new TXVodPlayer(mContext);
|
||||
mTXVodPlayConfig = new TXVodPlayConfig();
|
||||
mTXVodPlayConfig.setMaxCacheItems(15);
|
||||
mTXVodPlayConfig.setProgressInterval(200);
|
||||
mPlayer.setConfig(mTXVodPlayConfig);
|
||||
mPlayer.setAutoPlay(true);
|
||||
mPlayer.setVodListener(this);
|
||||
mPlayer.setPlayerView(mTXCloudVideoView);
|
||||
mTXCloudVideoView = (StyledPlayerView) findViewById(R.id.placeholderView);
|
||||
mTXCloudVideoView.setKeepContentOnPlayerReset(true);
|
||||
mTXCloudVideoView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIXED_WIDTH);
|
||||
mPlayer = new ExoPlayer.Builder(mContext).setLoadControl(control).build();
|
||||
mPlayer.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
|
||||
findViewById(R.id.root).setOnClickListener(this);
|
||||
mVideoCover = findViewById(R.id.video_cover);
|
||||
mPlayBtn = findViewById(R.id.btn_play);
|
||||
@@ -76,13 +87,59 @@ public class VideoPlayViewHolder extends AbsViewHolder implements ITXVodPlayList
|
||||
PropertyValuesHolder.ofFloat("alpha", 0f, 1f));
|
||||
mPlayBtnAnimator.setDuration(150);
|
||||
mPlayBtnAnimator.setInterpolator(new AccelerateInterpolator());
|
||||
mPlayer.addListener(new Player.Listener() {
|
||||
@Override
|
||||
public void onIsPlayingChanged(boolean isPlaying) {
|
||||
Player.Listener.super.onIsPlayingChanged(isPlaying);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlaybackStateChanged(int playbackState) {
|
||||
Player.Listener.super.onPlaybackStateChanged(playbackState);
|
||||
if (playbackState == Player.STATE_READY) {
|
||||
mPlayer.play();
|
||||
onPlayEvent(TXLiveConstants.PLAY_EVT_PLAY_BEGIN, null);
|
||||
} else if (playbackState == Player.STATE_BUFFERING) {
|
||||
onPlayEvent(TXLiveConstants.PLAY_EVT_PLAY_LOADING, null);
|
||||
} else if (playbackState == Player.STATE_ENDED) {
|
||||
onPlayEvent(TXLiveConstants.PLAY_EVT_PLAY_END, null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onVideoSizeChanged(VideoSize videoSize) {
|
||||
Player.Listener.super.onVideoSizeChanged(videoSize);
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putInt("EVT_PARAM1", videoSize.width);
|
||||
bundle.putInt("EVT_PARAM2", videoSize.height);
|
||||
onPlayEvent(TXLiveConstants.PLAY_EVT_CHANGE_RESOLUTION, bundle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRenderedFirstFrame() {
|
||||
Player.Listener.super.onRenderedFirstFrame();
|
||||
onPlayEvent(TXLiveConstants.PLAY_EVT_RCV_FIRST_I_FRAME, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerError(PlaybackException error) {
|
||||
Player.Listener.super.onPlayerError(error);
|
||||
LogUtils.e(error);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerErrorChanged(@Nullable PlaybackException error) {
|
||||
Player.Listener.super.onPlayerErrorChanged(error);
|
||||
LogUtils.e(error);
|
||||
}
|
||||
});
|
||||
mTXCloudVideoView.setPlayer(mPlayer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 播放器事件回调
|
||||
*/
|
||||
@Override
|
||||
public void onPlayEvent(TXVodPlayer txVodPlayer, int e, Bundle bundle) {
|
||||
public void onPlayEvent(int e, Bundle bundle) {
|
||||
switch (e) {
|
||||
case TXLiveConstants.PLAY_EVT_PLAY_BEGIN://加载完成,开始播放的回调
|
||||
mStartPlay = true;
|
||||
@@ -119,11 +176,6 @@ public class VideoPlayViewHolder extends AbsViewHolder implements ITXVodPlayList
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNetStatus(TXVodPlayer txVodPlayer, Bundle bundle) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取到视频宽高回调
|
||||
*/
|
||||
@@ -166,20 +218,8 @@ public class VideoPlayViewHolder extends AbsViewHolder implements ITXVodPlayList
|
||||
if (TextUtils.isEmpty(url)) {
|
||||
return;
|
||||
}
|
||||
if (mTXVodPlayConfig == null) {
|
||||
mTXVodPlayConfig = new TXVodPlayConfig();
|
||||
mTXVodPlayConfig.setMaxCacheItems(15);
|
||||
mTXVodPlayConfig.setProgressInterval(200);
|
||||
}
|
||||
if (url.endsWith(".m3u8")) {
|
||||
mTXVodPlayConfig.setCacheFolderPath(null);
|
||||
} else {
|
||||
mTXVodPlayConfig.setCacheFolderPath(mCachePath);
|
||||
}
|
||||
mPlayer.setConfig(mTXVodPlayConfig);
|
||||
if (mPlayer != null) {
|
||||
mPlayer.startPlay(url);
|
||||
}
|
||||
mPlayer.setMediaItem(MediaItem.fromUri(url));
|
||||
mPlayer.prepare();
|
||||
VideoHttpUtil.videoWatchStart(videoBean.getUid(), videoBean.getId());
|
||||
}
|
||||
|
||||
@@ -188,7 +228,7 @@ public class VideoPlayViewHolder extends AbsViewHolder implements ITXVodPlayList
|
||||
*/
|
||||
public void stopPlay() {
|
||||
if (mPlayer != null) {
|
||||
mPlayer.stopPlay(false);
|
||||
mPlayer.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,8 +237,8 @@ public class VideoPlayViewHolder extends AbsViewHolder implements ITXVodPlayList
|
||||
*/
|
||||
private void replay() {
|
||||
if (mPlayer != null) {
|
||||
mPlayer.seek(0);
|
||||
mPlayer.resume();
|
||||
mPlayer.seekTo(0);
|
||||
mPlayer.play();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,8 +246,8 @@ public class VideoPlayViewHolder extends AbsViewHolder implements ITXVodPlayList
|
||||
VideoHttpUtil.cancel(VideoHttpConsts.VIDEO_WATCH_START);
|
||||
VideoHttpUtil.cancel(VideoHttpConsts.VIDEO_WATCH_END);
|
||||
if (mPlayer != null) {
|
||||
mPlayer.stopPlay(false);
|
||||
mPlayer.setPlayListener(null);
|
||||
mPlayer.stop();
|
||||
mPlayer.release();
|
||||
}
|
||||
mPlayer = null;
|
||||
mActionListener = null;
|
||||
@@ -229,7 +269,7 @@ public class VideoPlayViewHolder extends AbsViewHolder implements ITXVodPlayList
|
||||
public void resumePlay() {
|
||||
if (mPaused) {
|
||||
if (!mClickPaused && mPlayer != null) {
|
||||
mPlayer.resume();
|
||||
mPlayer.play();
|
||||
}
|
||||
}
|
||||
mPaused = false;
|
||||
@@ -263,7 +303,7 @@ public class VideoPlayViewHolder extends AbsViewHolder implements ITXVodPlayList
|
||||
}
|
||||
if (mPlayer != null) {
|
||||
if (mClickPaused) {
|
||||
mPlayer.resume();
|
||||
mPlayer.play();
|
||||
} else {
|
||||
mPlayer.pause();
|
||||
}
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/root"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<com.tencent.rtmp.ui.TXCloudVideoView
|
||||
android:id="@+id/video_view"
|
||||
<com.google.android.exoplayer2.ui.StyledPlayerView
|
||||
android:id="@+id/placeholderView"
|
||||
app:surface_type="texture_view"
|
||||
app:use_controller="false"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_centerInParent="true" />
|
||||
@@ -14,7 +18,7 @@
|
||||
android:id="@+id/video_cover"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="#000" />
|
||||
android:background="@color/transparent" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/btn_play"
|
||||
|
||||
Reference in New Issue
Block a user