|
@@ -1,9 +1,5 @@
|
|
|
package com.tencent.liteav.demo.superplayer.player;
|
|
|
|
|
|
-import static com.tencent.liteav.demo.superplayer.SuperPlayerModel.PLAY_ACTION_AUTO_PLAY;
|
|
|
-import static com.tencent.liteav.demo.superplayer.SuperPlayerModel.PLAY_ACTION_MANUAL_PLAY;
|
|
|
-import static com.tencent.liteav.demo.superplayer.SuperPlayerModel.PLAY_ACTION_PRELOAD;
|
|
|
-
|
|
|
import android.content.Context;
|
|
|
import android.os.Bundle;
|
|
|
import android.text.TextUtils;
|
|
@@ -13,19 +9,16 @@ import com.tencent.liteav.demo.superplayer.SuperPlayerCode;
|
|
|
import com.tencent.liteav.demo.superplayer.SuperPlayerDef;
|
|
|
import com.tencent.liteav.demo.superplayer.SuperPlayerGlobalConfig;
|
|
|
import com.tencent.liteav.demo.superplayer.SuperPlayerModel;
|
|
|
-import com.tencent.rtmp.ITXLivePlayListener;
|
|
|
import com.tencent.rtmp.ITXVodPlayListener;
|
|
|
import com.tencent.rtmp.TXLiveBase;
|
|
|
import com.tencent.rtmp.TXLiveConstants;
|
|
|
-import com.tencent.rtmp.TXLivePlayConfig;
|
|
|
-import com.tencent.rtmp.TXLivePlayer;
|
|
|
import com.tencent.rtmp.TXVodPlayConfig;
|
|
|
import com.tencent.rtmp.TXVodPlayer;
|
|
|
import com.tencent.rtmp.ui.TXCloudVideoView;
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
-public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLivePlayListener {
|
|
|
+public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener {
|
|
|
|
|
|
private static final String TAG = "SuperPlayerImpl";
|
|
|
private static final int SUPERPLAYER_MODE = 1;
|
|
@@ -38,91 +31,26 @@ public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLive
|
|
|
private TXVodPlayer mVodPlayer; // 点播播放器
|
|
|
private TXVodPlayConfig mVodPlayConfig; // 点播播放器配置
|
|
|
|
|
|
- private ISuperPlayerListener mSuperPlayerListener;
|
|
|
private SuperPlayerModel mCurrentModel; // 当前播放的model
|
|
|
private SuperPlayerObserver mObserver;
|
|
|
private SuperPlayerDef.PlayerMode mCurrentPlayMode = SuperPlayerDef.PlayerMode.WINDOW; // 当前播放模式
|
|
|
private SuperPlayerDef.PlayerState mCurrentPlayState = SuperPlayerDef.PlayerState.INIT;
|
|
|
|
|
|
private String mCurrentPlayVideoURL; // 当前播放的URL
|
|
|
- private int mSeekPos; // 记录切换硬解时的播放时间
|
|
|
private float mStartPos; // 视频开始播放时间
|
|
|
- private long mReportLiveStartTime = -1; // 直播开始时间,用于上报使用时长
|
|
|
- private long mReportVodStartTime = -1; // 点播开始时间,用于上报使用时长
|
|
|
- private long mMaxLiveProgressTime; // 观看直播的最大时长
|
|
|
- private boolean mIsAutoPlay = true; // 是否自动播放
|
|
|
- private boolean mChangeHWAcceleration; // 切换硬解后接收到第一个关键帧前的标记位
|
|
|
- private int mPlayAction; //播放模式
|
|
|
+
|
|
|
private boolean isPrepared = false;
|
|
|
private boolean isNeedResume = false;
|
|
|
private boolean mNeedToPause = false;
|
|
|
+ private boolean mTimeout = false;
|
|
|
|
|
|
public SuperPlayerImpl(Context context, TXCloudVideoView videoView) {
|
|
|
- initialize(context, videoView);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 直播播放器事件回调
|
|
|
- *
|
|
|
- * @param event
|
|
|
- * @param param
|
|
|
- */
|
|
|
- @Override
|
|
|
- public void onPlayEvent(int event, Bundle param) {
|
|
|
- if (event != TXLiveConstants.PLAY_EVT_PLAY_PROGRESS) {
|
|
|
- String playEventLog = "TXLivePlayer onPlayEvent event: " + event + ", "
|
|
|
- + param.getString(TXLiveConstants.EVT_DESCRIPTION);
|
|
|
- Log.d(TAG, playEventLog);
|
|
|
- }
|
|
|
- switch (event) {
|
|
|
- case TXLiveConstants.PLAY_EVT_VOD_PLAY_PREPARED: //视频播放开始
|
|
|
- case TXLiveConstants.PLAY_EVT_PLAY_BEGIN:
|
|
|
- updatePlayerState(SuperPlayerDef.PlayerState.PLAYING);
|
|
|
- break;
|
|
|
- case TXLiveConstants.PLAY_ERR_NET_DISCONNECT:
|
|
|
- case TXLiveConstants.PLAY_EVT_PLAY_END:
|
|
|
- stop();
|
|
|
- updatePlayerState(SuperPlayerDef.PlayerState.END);
|
|
|
- if (event == TXLiveConstants.PLAY_ERR_NET_DISCONNECT) {
|
|
|
- onError(SuperPlayerCode.NET_ERROR, "网络不给力,点击重试");
|
|
|
- } else {
|
|
|
- onError(SuperPlayerCode.LIVE_PLAY_END, param.getString(TXLiveConstants.EVT_DESCRIPTION));
|
|
|
- }
|
|
|
- break;
|
|
|
- case TXLiveConstants.PLAY_EVT_PLAY_LOADING:
|
|
|
-// case TXLiveConstants.PLAY_WARNING_RECONNECT: //暂时去掉,回调该状态时,播放画面可能是正常的,loading 状态只在 TXLiveConstants.PLAY_EVT_PLAY_LOADING 处理
|
|
|
- updatePlayerState(SuperPlayerDef.PlayerState.LOADING);
|
|
|
- break;
|
|
|
- case TXLiveConstants.PLAY_EVT_RCV_FIRST_I_FRAME:
|
|
|
- updatePlayerState(SuperPlayerDef.PlayerState.PLAYING);
|
|
|
- mObserver.onRcvFirstIframe();
|
|
|
- break;
|
|
|
- case TXLiveConstants.PLAY_EVT_PLAY_PROGRESS:
|
|
|
- int progress = param.getInt(TXLiveConstants.EVT_PLAY_PROGRESS_MS);
|
|
|
- mMaxLiveProgressTime = progress > mMaxLiveProgressTime ? progress : mMaxLiveProgressTime;
|
|
|
- updatePlayProgress(progress / 1000, mMaxLiveProgressTime / 1000);
|
|
|
- break;
|
|
|
- default:
|
|
|
- break;
|
|
|
- }
|
|
|
- if (mSuperPlayerListener != null) {
|
|
|
- mSuperPlayerListener.onLivePlayEvent(event, param);
|
|
|
- }
|
|
|
- }
|
|
|
+ mContext = context;
|
|
|
+ mVideoView = videoView;
|
|
|
|
|
|
- /**
|
|
|
- * 直播播放器网络状态回调
|
|
|
- *
|
|
|
- * @param bundle
|
|
|
- */
|
|
|
- @Override
|
|
|
- public void onNetStatus(Bundle bundle) {
|
|
|
- if (mSuperPlayerListener != null) {
|
|
|
- mSuperPlayerListener.onLiveNetStatus(bundle);
|
|
|
- }
|
|
|
+ initVodPlayer(mContext);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
@Override
|
|
|
public void setNeedToPause(boolean value) {
|
|
|
mNeedToPause = value;
|
|
@@ -151,11 +79,6 @@ public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLive
|
|
|
if (mNeedToPause) {
|
|
|
return;
|
|
|
}
|
|
|
- if (mChangeHWAcceleration) { //切换软硬解码器后,重新seek位置
|
|
|
- Log.i(TAG, "seek pos:" + mSeekPos);
|
|
|
- seek(mSeekPos);
|
|
|
- mChangeHWAcceleration = false;
|
|
|
- }
|
|
|
updatePlayerState(SuperPlayerDef.PlayerState.PLAYING);
|
|
|
mObserver.onRcvFirstIframe();
|
|
|
break;
|
|
@@ -189,17 +112,13 @@ public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLive
|
|
|
updatePlayerState(SuperPlayerDef.PlayerState.PAUSE);
|
|
|
onError(SuperPlayerCode.VOD_PLAY_FAIL, param.getString(TXLiveConstants.EVT_DESCRIPTION));
|
|
|
}
|
|
|
- if (mSuperPlayerListener != null) {
|
|
|
- mSuperPlayerListener.onVodPlayEvent(player, event, param);
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
private void onVodPlayPrepared() {
|
|
|
- Log.i(TAG, "PLAY_EVT_VOD_PLAY_PREPARED");
|
|
|
isPrepared = true;
|
|
|
|
|
|
if (mNeedToPause) {
|
|
|
- pauseVod();
|
|
|
+ pause();
|
|
|
return;
|
|
|
}
|
|
|
if (isNeedResume) {
|
|
@@ -215,16 +134,7 @@ public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLive
|
|
|
*/
|
|
|
@Override
|
|
|
public void onNetStatus(TXVodPlayer player, Bundle bundle) {
|
|
|
- if (mSuperPlayerListener != null) {
|
|
|
- mSuperPlayerListener.onVodNetStatus(player, bundle);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void initialize(Context context, TXCloudVideoView videoView) {
|
|
|
- mContext = context;
|
|
|
- mVideoView = videoView;
|
|
|
-
|
|
|
- initVodPlayer(mContext);
|
|
|
+ // DO NOTHING
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -260,12 +170,7 @@ public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLive
|
|
|
*/
|
|
|
public void playWithModel(SuperPlayerModel model) {
|
|
|
reset();
|
|
|
- if (!TextUtils.isEmpty(model.url)) {
|
|
|
- playWithUrl(model);
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- private void playWithUrl(SuperPlayerModel model) {
|
|
|
String videoURL = model.url;
|
|
|
|
|
|
if (TextUtils.isEmpty(videoURL)) {
|
|
@@ -273,25 +178,12 @@ public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLive
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // 点播播放器:播放点播文件
|
|
|
- mReportVodStartTime = System.currentTimeMillis();
|
|
|
mVodPlayer.setPlayerView(mVideoView);
|
|
|
playVodURL(videoURL);
|
|
|
|
|
|
updatePlayProgress(0, model.duration);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 播放视频
|
|
|
- *
|
|
|
- * @param model
|
|
|
- */
|
|
|
- private void playModeVideo(SuperPlayerModel model) {
|
|
|
- if (!TextUtils.isEmpty(model.url)) {
|
|
|
- playVodURL(model.url);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 播放点播url
|
|
|
*/
|
|
@@ -303,55 +195,13 @@ public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLive
|
|
|
|
|
|
if (mVodPlayer != null) {
|
|
|
mVodPlayer.setStartTime(mStartPos);
|
|
|
- mVodPlayer.setAutoPlay(mIsAutoPlay);
|
|
|
- if (mPlayAction == PLAY_ACTION_AUTO_PLAY || mPlayAction == PLAY_ACTION_MANUAL_PLAY) {
|
|
|
- mVodPlayer.setAutoPlay(true);
|
|
|
- } else if (mPlayAction == PLAY_ACTION_PRELOAD) {
|
|
|
- mVodPlayer.setAutoPlay(false);
|
|
|
- mPlayAction = PLAY_ACTION_AUTO_PLAY;
|
|
|
- }
|
|
|
+ mVodPlayer.setAutoPlay(true);
|
|
|
mVodPlayer.setVodListener(this);
|
|
|
mVodPlayer.setToken(null);
|
|
|
mVodPlayer.startPlay(url);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private boolean isVersionSupportAppendUrl() {
|
|
|
- String strVersion = TXLiveBase.getSDKVersionStr();
|
|
|
- String[] strVers = strVersion.split("\\.");
|
|
|
- if (strVers.length <= 1) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- int majorVer = 0;
|
|
|
- int minorVer = 0;
|
|
|
- try {
|
|
|
- majorVer = Integer.parseInt(strVers[0]);
|
|
|
- minorVer = Integer.parseInt(strVers[1]);
|
|
|
- } catch (NumberFormatException e) {
|
|
|
- Log.e(TAG, "parse version failed.", e);
|
|
|
- majorVer = 0;
|
|
|
- minorVer = 0;
|
|
|
- }
|
|
|
- Log.i(TAG, strVersion + " , " + majorVer + " , " + minorVer);
|
|
|
- return majorVer > SUPPORT_MAJOR_VERSION || (majorVer == SUPPORT_MAJOR_VERSION && minorVer >= SUPPORT_MINOR_VERSION);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 上报播放时长
|
|
|
- */
|
|
|
- private void reportPlayTime() {
|
|
|
- if (mReportLiveStartTime != -1) {
|
|
|
- long reportEndTime = System.currentTimeMillis();
|
|
|
- long diff = (reportEndTime - mReportLiveStartTime) / 1000;
|
|
|
- mReportLiveStartTime = -1;
|
|
|
- }
|
|
|
- if (mReportVodStartTime != -1) {
|
|
|
- long reportEndTime = System.currentTimeMillis();
|
|
|
- long diff = (reportEndTime - mReportVodStartTime) / 1000;
|
|
|
- mReportVodStartTime = -1;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 更新播放进度
|
|
|
*
|
|
@@ -407,36 +257,18 @@ public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLive
|
|
|
return title;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 是否是RTMP协议
|
|
|
- *
|
|
|
- * @param videoURL
|
|
|
- * @return
|
|
|
- */
|
|
|
- private boolean isRTMPPlay(String videoURL) {
|
|
|
- return !TextUtils.isEmpty(videoURL) && videoURL.startsWith("rtmp");
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 是否是HTTP-FLV协议
|
|
|
- *
|
|
|
- * @param videoURL
|
|
|
- * @return
|
|
|
- */
|
|
|
- private boolean isFLVPlay(String videoURL) {
|
|
|
- return (!TextUtils.isEmpty(videoURL) && videoURL.startsWith("http://")
|
|
|
- || videoURL.startsWith("https://")) && videoURL.contains(".flv");
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public void play(SuperPlayerModel model) {
|
|
|
- mPlayAction = model.playAction;
|
|
|
+ if (mTimeout) return;
|
|
|
+
|
|
|
mCurrentModel = model;
|
|
|
playWithModel(model);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void restart() {
|
|
|
+ if (mTimeout) return;
|
|
|
+
|
|
|
if (mCurrentPlayVideoURL != null) {
|
|
|
playVodURL(mCurrentPlayVideoURL);
|
|
|
} else {
|
|
@@ -450,14 +282,10 @@ public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLive
|
|
|
updatePlayerState(SuperPlayerDef.PlayerState.PAUSE);
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public void pauseVod() {
|
|
|
- mVodPlayer.pause();
|
|
|
- updatePlayerState(SuperPlayerDef.PlayerState.PAUSE);
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public void resume() {
|
|
|
+ if (mTimeout) return;
|
|
|
+
|
|
|
isNeedResume = true;
|
|
|
if (isPrepared) {
|
|
|
mVodPlayer.resume();
|
|
@@ -478,6 +306,13 @@ public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLive
|
|
|
updatePlayerState(SuperPlayerDef.PlayerState.INIT);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void setTimeout(boolean timeout) {
|
|
|
+ this.mTimeout = timeout;
|
|
|
+
|
|
|
+ if(timeout) pause();
|
|
|
+ }
|
|
|
+
|
|
|
private void resetPlayer() {
|
|
|
isPrepared = false;
|
|
|
isNeedResume = false;
|
|
@@ -485,12 +320,6 @@ public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLive
|
|
|
mVodPlayer.setVodListener(null);
|
|
|
mVodPlayer.stopPlay(false);
|
|
|
}
|
|
|
- reportPlayTime();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void destroy() {
|
|
|
-
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -499,27 +328,14 @@ public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLive
|
|
|
return;
|
|
|
}
|
|
|
mCurrentPlayMode = playerMode;
|
|
|
- }
|
|
|
|
|
|
- @Override
|
|
|
- public void enableHardwareDecode(boolean enable) {
|
|
|
- mVodPlayer.enableHardwareDecode(enable);
|
|
|
- if (mCurrentPlayState != SuperPlayerDef.PlayerState.END) {
|
|
|
- mChangeHWAcceleration = true;
|
|
|
- mSeekPos = (int) mVodPlayer.getCurrentPlaybackTime();
|
|
|
- Log.i(TAG, "save pos:" + mSeekPos);
|
|
|
- resetPlayer();
|
|
|
- playModeVideo(mCurrentModel);
|
|
|
+ if (mCurrentPlayMode == SuperPlayerDef.PlayerMode.FULLSCREEN) {
|
|
|
+ if (getPlayerState() == SuperPlayerDef.PlayerState.PAUSE) {
|
|
|
+ resume();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public void setPlayerView(TXCloudVideoView videoView) {
|
|
|
- mVideoView = videoView;
|
|
|
- mVodPlayer.setPlayerView(videoView);
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public void seek(int position) {
|
|
|
if (mVodPlayer != null) {
|
|
@@ -534,16 +350,6 @@ public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLive
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public void setRate(float speedLevel) {
|
|
|
- mVodPlayer.setRate(speedLevel);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String getPlayURL() {
|
|
|
- return mCurrentPlayVideoURL;
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public SuperPlayerDef.PlayerMode getPlayerMode() {
|
|
|
return mCurrentPlayMode;
|
|
@@ -559,11 +365,6 @@ public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLive
|
|
|
mObserver = observer;
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public void setSuperPlayerListener(ISuperPlayerListener superPlayerListener) {
|
|
|
- mSuperPlayerListener = superPlayerListener;
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public void setLoop(boolean isLoop) {
|
|
|
mVodPlayer.setLoop(isLoop);
|
|
@@ -574,10 +375,4 @@ public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLive
|
|
|
this.mStartPos = startPos;
|
|
|
mVodPlayer.setStartTime(startPos);
|
|
|
}
|
|
|
-
|
|
|
- @Override
|
|
|
- public void setAutoPlay(boolean isAutoPlay) {
|
|
|
- this.mIsAutoPlay = isAutoPlay;
|
|
|
- mVodPlayer.setAutoPlay(isAutoPlay);
|
|
|
- }
|
|
|
}
|