|
@@ -6,8 +6,8 @@ import android.util.Log;
|
|
|
import android.view.KeyEvent;
|
|
|
import android.view.View;
|
|
|
import android.view.ViewGroup;
|
|
|
-import android.webkit.WebView;
|
|
|
import android.widget.FrameLayout;
|
|
|
+import android.widget.ImageView;
|
|
|
import android.widget.LinearLayout;
|
|
|
import android.widget.RelativeLayout;
|
|
|
import android.widget.SeekBar;
|
|
@@ -15,6 +15,7 @@ import android.widget.TextView;
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
import com.hc.lib.R;
|
|
|
+import com.hc.lib.store.LastPlayTimeStore;
|
|
|
import com.jewel.mplayer.ControlView;
|
|
|
import com.jewel.mplayer.CoreVideoView;
|
|
|
import com.jewel.mplayer.OnVideoChangeListener;
|
|
@@ -44,10 +45,8 @@ public class MVideoPlayer extends FrameLayout implements IVideoPlayer {
|
|
|
private boolean needSeek = false;
|
|
|
private SeekBar progressBar;
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
private VideoHideControlViewRunnable videoHideControlViewRunnable;
|
|
|
+ private VideoHideLastPlayBarRunnable videoHideLastPlayBarRunnable;
|
|
|
private OnPlayerPausedListener onPlayerPausedListener;
|
|
|
|
|
|
//add by 许林 2018/12/5
|
|
@@ -58,7 +57,7 @@ public class MVideoPlayer extends FrameLayout implements IVideoPlayer {
|
|
|
TextView percentText;
|
|
|
|
|
|
//延迟滑动计时器
|
|
|
- private Timer timer ;
|
|
|
+ private Timer timer;
|
|
|
private TimerTask timerTask;
|
|
|
|
|
|
private VideoData currentVideoData;
|
|
@@ -66,6 +65,16 @@ public class MVideoPlayer extends FrameLayout implements IVideoPlayer {
|
|
|
//页面activity
|
|
|
Activity mainActivity;
|
|
|
|
|
|
+ //本地存储
|
|
|
+ LastPlayTimeStore lastPlayTimeStore;
|
|
|
+ String mLastPlayTime = "";
|
|
|
+ static boolean isShowLastPlayBar = false;
|
|
|
+
|
|
|
+ //上次退出界面
|
|
|
+ LinearLayout lastPlayBar;
|
|
|
+ TextView lastTimeText;
|
|
|
+ ImageView resumePlayButton;
|
|
|
+
|
|
|
private int seekPercent = 5; //快进百分比
|
|
|
|
|
|
private String TAG = "MVideoPlayer";
|
|
@@ -78,7 +87,7 @@ public class MVideoPlayer extends FrameLayout implements IVideoPlayer {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public MVideoPlayer(Context context) {
|
|
|
+ public MVideoPlayer(final Context context) {
|
|
|
super(context);
|
|
|
|
|
|
View root = View.inflate(getContext(), R.layout.layout_m_player, this);
|
|
@@ -97,6 +106,11 @@ public class MVideoPlayer extends FrameLayout implements IVideoPlayer {
|
|
|
seekBar = findViewById(R.id.seek_bar_tv);
|
|
|
percentText = findViewById(R.id.percent_text);
|
|
|
|
|
|
+ //上次退出界面 add by 许林 2019/2/26
|
|
|
+ lastPlayBar = findViewById(R.id.last_play_bar);
|
|
|
+ lastTimeText = findViewById(R.id.last_time_text);
|
|
|
+ resumePlayButton = findViewById(R.id.resume_play_btn);
|
|
|
+
|
|
|
|
|
|
seekBar.setMax(1000);
|
|
|
seekBar.setProgress(0);
|
|
@@ -139,7 +153,27 @@ public class MVideoPlayer extends FrameLayout implements IVideoPlayer {
|
|
|
videoView.addOnVideoChangeListener(new OnVideoChangeListener() {
|
|
|
@Override
|
|
|
public void onVideoPrepared(CoreVideoView coreVideoView) {
|
|
|
+ Log.d(TAG,"lastPlayTimeInt:" + mLastPlayTime);
|
|
|
isPrepared = true;
|
|
|
+ if(isFullVideo && !mLastPlayTime.isEmpty()){
|
|
|
+ new Timer().schedule(new TimerTask() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ Activity activity = (Activity)context;
|
|
|
+ activity.runOnUiThread(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ int lastPlayTimeInt = Integer.parseInt(mLastPlayTime);
|
|
|
+ Log.d(TAG,"lastPlayTimeInt:" + lastPlayTimeInt);
|
|
|
+ videoView.seekTo(lastPlayTimeInt);
|
|
|
+ showLastTimeBar(mLastPlayTime);
|
|
|
+ hideAllWidget();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ },200);
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -216,9 +250,11 @@ public class MVideoPlayer extends FrameLayout implements IVideoPlayer {
|
|
|
|
|
|
}
|
|
|
});
|
|
|
+
|
|
|
+ lastPlayTimeStore = new LastPlayTimeStore(context);
|
|
|
}
|
|
|
|
|
|
- public void start(VideoData videoData) {
|
|
|
+ public void start(final VideoData videoData) {
|
|
|
currentVideoData = videoData;
|
|
|
if (videoData != null) {
|
|
|
onVideoDestroy();
|
|
@@ -228,8 +264,17 @@ public class MVideoPlayer extends FrameLayout implements IVideoPlayer {
|
|
|
}
|
|
|
videoView.start();
|
|
|
controlView.setTitle(videoData.getTitle());
|
|
|
+ //获取上次播放时间,add by xulin 2019/2/26
|
|
|
if (isFullVideo) {
|
|
|
- showControlWidget();
|
|
|
+ String lastPlayTime = getLastPlayTime(videoData.getId());
|
|
|
+ //Log.d(TAG,"lastPlayTime:" + lastPlayTime + "; sourceId:" + videoData.getId());
|
|
|
+ lastPlayTimeStore.delete(videoData.getId());
|
|
|
+ if(!lastPlayTime.isEmpty()){
|
|
|
+ mLastPlayTime = lastPlayTime;
|
|
|
+ }else{
|
|
|
+ showControlWidget();
|
|
|
+ mLastPlayTime= "";
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
if(layoutLoading != null){
|
|
@@ -237,6 +282,16 @@ public class MVideoPlayer extends FrameLayout implements IVideoPlayer {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public void resumePlay(){
|
|
|
+ lastPlayBar.setVisibility(INVISIBLE);
|
|
|
+ isShowLastPlayBar = false;
|
|
|
+ videoView.seekTo(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getLastPlayTime(String sourceId){
|
|
|
+ return lastPlayTimeStore.getTimeBySourceId(sourceId);
|
|
|
+ }
|
|
|
+
|
|
|
public void start() {
|
|
|
videoView.start();
|
|
|
}
|
|
@@ -278,10 +333,34 @@ public class MVideoPlayer extends FrameLayout implements IVideoPlayer {
|
|
|
* by xuling 20180726
|
|
|
*/
|
|
|
public int getDuration(){
|
|
|
- if(!isPrepared){
|
|
|
+ try{
|
|
|
+ if(!isPrepared){
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ return videoView.getDuration();
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前播放时间(毫秒)
|
|
|
+ * by xuling 20180726
|
|
|
+ */
|
|
|
+ public int getCurrentPos(){
|
|
|
+ try{
|
|
|
+ if(!isPrepared){
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ return videoView.getCurrentPositionWhenPlaying();
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
return 0;
|
|
|
}
|
|
|
- return videoView.getDuration();
|
|
|
|
|
|
}
|
|
|
|
|
@@ -289,6 +368,18 @@ public class MVideoPlayer extends FrameLayout implements IVideoPlayer {
|
|
|
* 延迟执行视频快进任务
|
|
|
* by xuling 20180726
|
|
|
*/
|
|
|
+ public void storeLastPlayTime(){
|
|
|
+ int currentPos = getCurrentPos();
|
|
|
+ int duration = getDuration();
|
|
|
+ if(currentPos < duration){
|
|
|
+ lastPlayTimeStore.insert(String.valueOf(currentPos),currentVideoData.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 延迟执行视频快进任务
|
|
|
+ * by xuling 20180726
|
|
|
+ */
|
|
|
private void initTimer(){
|
|
|
if(mainActivity == null){
|
|
|
Log.d(TAG,"请向播放器传递activity实例");
|
|
@@ -306,7 +397,10 @@ public class MVideoPlayer extends FrameLayout implements IVideoPlayer {
|
|
|
long myCurrentTime = Calendar.getInstance().getTimeInMillis();
|
|
|
if(myCurrentTime - currentTime > 3){
|
|
|
int position = seekBar.getProgress();
|
|
|
- position = position* getDuration()/1000;
|
|
|
+ int duration = getDuration();
|
|
|
+ float fPostion = position;
|
|
|
+ float fDuration = duration;
|
|
|
+ position = (int)(fPostion*fDuration/1000);
|
|
|
seekTo(position);
|
|
|
seekContainer.setVisibility(GONE);
|
|
|
}
|
|
@@ -315,7 +409,7 @@ public class MVideoPlayer extends FrameLayout implements IVideoPlayer {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- timer.schedule(timerTask,3000);
|
|
|
+ timer.schedule(timerTask,2000);
|
|
|
}
|
|
|
|
|
|
/* 跳转
|
|
@@ -330,16 +424,18 @@ public class MVideoPlayer extends FrameLayout implements IVideoPlayer {
|
|
|
Logger.d("HCVideoPlayer onKeyDown start: %s, %s", keyCode, event.getAction());
|
|
|
switch (keyCode) {
|
|
|
case KeyEvent.KEYCODE_DPAD_LEFT:
|
|
|
- /*if (event.getAction() == KeyEvent.ACTION_DOWN) {
|
|
|
+ /* if (event.getAction() == KeyEvent.ACTION_DOWN) {
|
|
|
changeProgress(true);
|
|
|
} else if (event.getAction() == KeyEvent.ACTION_UP) {
|
|
|
isChangingProgress = false;
|
|
|
}*/
|
|
|
- if(isPrepared){
|
|
|
- seekBack();
|
|
|
- }else{
|
|
|
- Toast.makeText(mainActivity,"视频准备中,暂时不能拖动快进",Toast.LENGTH_SHORT).show();
|
|
|
- }
|
|
|
+ if(!isShowLastPlayBar){
|
|
|
+ if(isPrepared){
|
|
|
+ seekBack();
|
|
|
+ }else{
|
|
|
+ Toast.makeText(mainActivity,"视频准备中,暂时不能拖动快进",Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+ }
|
|
|
return true;
|
|
|
case KeyEvent.KEYCODE_DPAD_RIGHT:
|
|
|
// if(isChangingProgress) {
|
|
@@ -348,32 +444,42 @@ public class MVideoPlayer extends FrameLayout implements IVideoPlayer {
|
|
|
// seekToTime = 10 * 60 * 1000;
|
|
|
// }
|
|
|
// }
|
|
|
- /* if (event.getAction() == KeyEvent.ACTION_DOWN) {
|
|
|
+ /* if (event.getAction() == KeyEvent.ACTION_DOWN) {
|
|
|
changeProgress(false);
|
|
|
} else if (event.getAction() == KeyEvent.ACTION_UP) {
|
|
|
isChangingProgress = false;
|
|
|
}*/
|
|
|
- if(isPrepared){
|
|
|
- seekForward();
|
|
|
- }else{
|
|
|
- Toast.makeText(mainActivity,"视频准备中,暂时不能拖动快进",Toast.LENGTH_SHORT).show();
|
|
|
+ if(!isShowLastPlayBar){
|
|
|
+ if(isPrepared){
|
|
|
+ seekForward();
|
|
|
+ }else{
|
|
|
+ Toast.makeText(mainActivity,"视频准备中,暂时不能拖动快进",Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
}
|
|
|
return true;
|
|
|
case KeyEvent.KEYCODE_DPAD_UP:
|
|
|
- showControlWidget();
|
|
|
+ if(!isShowLastPlayBar){
|
|
|
+ showControlWidget();
|
|
|
+ }
|
|
|
// changeVolume(true);
|
|
|
break;
|
|
|
case KeyEvent.KEYCODE_DPAD_DOWN:
|
|
|
- showControlWidget();
|
|
|
+ if(!isShowLastPlayBar){
|
|
|
+ showControlWidget();
|
|
|
+ }
|
|
|
// changeVolume(false);
|
|
|
break;
|
|
|
case KeyEvent.KEYCODE_DPAD_CENTER:
|
|
|
- if (event.getAction() == KeyEvent.ACTION_DOWN) {
|
|
|
- pauseOrPlay(false);
|
|
|
+ if(isShowLastPlayBar){
|
|
|
+ resumePlay();
|
|
|
+ }else{
|
|
|
+ if (event.getAction() == KeyEvent.ACTION_DOWN ) {
|
|
|
+ pauseOrPlay(false);
|
|
|
+ }
|
|
|
}
|
|
|
break;
|
|
|
case KeyEvent.KEYCODE_ENTER:
|
|
|
- if (event.getAction() == KeyEvent.ACTION_DOWN) {
|
|
|
+ if (event.getAction() == KeyEvent.ACTION_DOWN ) {
|
|
|
pauseOrPlay(false);
|
|
|
}
|
|
|
break;
|
|
@@ -384,28 +490,33 @@ public class MVideoPlayer extends FrameLayout implements IVideoPlayer {
|
|
|
return super.onKeyDown(keyCode, event);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void keyCodeBack(){
|
|
|
+ if(isShowLastPlayBar){
|
|
|
+ lastPlayBar.setVisibility(INVISIBLE);
|
|
|
+ isShowLastPlayBar = false;
|
|
|
+ }else{
|
|
|
+ pauseOrPlay(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
//add by 许林 ,2018/12/5
|
|
|
private void seekBack(){
|
|
|
- /*if(vipState == -1){
|
|
|
- if(isFee != 1){
|
|
|
- return;
|
|
|
- }
|
|
|
- }*/
|
|
|
-
|
|
|
if(seekContainer.getVisibility() != VISIBLE){
|
|
|
mainActivity.runOnUiThread(new Runnable() {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
hideAllWidget();
|
|
|
seekContainer.setVisibility(VISIBLE);
|
|
|
- int position = (videoView.getCurrentPositionWhenPlaying() * 1000)/ videoView.getDuration();
|
|
|
+ int currentPos = videoView.getCurrentPositionWhenPlaying();
|
|
|
+ int durationSec = videoView.getDuration()/1000;
|
|
|
+ int position = currentPos/ durationSec;
|
|
|
seekBar.setProgress(position);
|
|
|
currentTime =Calendar.getInstance().getTimeInMillis();
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
-
|
|
|
int position = seekBar.getProgress() - seekPercent* 10;
|
|
|
seek(position);
|
|
|
}
|
|
@@ -423,14 +534,15 @@ public class MVideoPlayer extends FrameLayout implements IVideoPlayer {
|
|
|
public void run() {
|
|
|
hideAllWidget();
|
|
|
seekContainer.setVisibility(VISIBLE);
|
|
|
- int position = (videoView.getCurrentPositionWhenPlaying() * 1000)/ videoView.getDuration();
|
|
|
+ int currentPos = videoView.getCurrentPositionWhenPlaying();
|
|
|
+ int durationSec = videoView.getDuration()/1000;
|
|
|
+ int position = currentPos/ durationSec;
|
|
|
seekBar.setProgress(position);
|
|
|
currentTime =Calendar.getInstance().getTimeInMillis();
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
-
|
|
|
int position = seekBar.getProgress() + seekPercent* 10;
|
|
|
seek(position);
|
|
|
}
|
|
@@ -463,11 +575,66 @@ public class MVideoPlayer extends FrameLayout implements IVideoPlayer {
|
|
|
Log.d(TAG,"hideAllWidget,layoutLoading gone");
|
|
|
}
|
|
|
|
|
|
+ /*
|
|
|
+ * 显示上次退出播放的时间
|
|
|
+ * */
|
|
|
+ private void showLastTimeBar(String lastTime){
|
|
|
+ lastPlayBar.setVisibility(VISIBLE);
|
|
|
+ //resumePlayButton.requestFocus();
|
|
|
+ isShowLastPlayBar = true;
|
|
|
+ int lastTimeCount = Integer.parseInt(lastTime)/1000;
|
|
|
+ String lastTimeToShow = "上次观看到" + secToTime(lastTimeCount)+ ",已为您继续播放";
|
|
|
+ lastTimeText.setText(lastTimeToShow);
|
|
|
+
|
|
|
+ if (videoHideLastPlayBarRunnable == null) {
|
|
|
+ videoHideLastPlayBarRunnable = new VideoHideLastPlayBarRunnable(lastPlayBar);
|
|
|
+ }
|
|
|
+ getRootView().getHandler().removeCallbacks(videoHideLastPlayBarRunnable);
|
|
|
+ getRootView().getHandler().postDelayed(videoHideLastPlayBarRunnable, 5000);
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 将秒转化为时秒分
|
|
|
+ * */
|
|
|
+ public static String secToTime(int time) {
|
|
|
+ String timeStr = null;
|
|
|
+ int hour = 0;
|
|
|
+ int minute = 0;
|
|
|
+ int second = 0;
|
|
|
+ if (time <= 0)
|
|
|
+ return "00:00";
|
|
|
+ else {
|
|
|
+ minute = time / 60;
|
|
|
+ if (minute < 60) {
|
|
|
+ second = time % 60;
|
|
|
+ timeStr = unitFormat(minute) + ":" + unitFormat(second);
|
|
|
+ } else {
|
|
|
+ hour = minute / 60;
|
|
|
+ if (hour > 99)
|
|
|
+ return "99:59:59";
|
|
|
+ minute = minute % 60;
|
|
|
+ second = time - hour * 3600 - minute * 60;
|
|
|
+ timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return timeStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String unitFormat(int i) {
|
|
|
+ String retStr = null;
|
|
|
+ if (i >= 0 && i < 10)
|
|
|
+ retStr = "0" + Integer.toString(i);
|
|
|
+ else
|
|
|
+ retStr = "" + i;
|
|
|
+ return retStr;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 显示控制器
|
|
|
*/
|
|
|
private void showControlWidget() {
|
|
|
-
|
|
|
layoutTop.setVisibility(VISIBLE);
|
|
|
layoutBottom.setVisibility(VISIBLE);
|
|
|
|
|
@@ -514,7 +681,9 @@ public class MVideoPlayer extends FrameLayout implements IVideoPlayer {
|
|
|
}
|
|
|
|
|
|
public void hideSeekContainer(){
|
|
|
- seekContainer.setVisibility(INVISIBLE);
|
|
|
+ if(seekContainer != null){
|
|
|
+ seekContainer.setVisibility(INVISIBLE);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -561,6 +730,8 @@ public class MVideoPlayer extends FrameLayout implements IVideoPlayer {
|
|
|
start(currentVideoData);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 视频控制视图消失线程
|
|
|
*/
|
|
@@ -579,6 +750,22 @@ public class MVideoPlayer extends FrameLayout implements IVideoPlayer {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private static class VideoHideLastPlayBarRunnable implements Runnable {
|
|
|
+
|
|
|
+ private View view;
|
|
|
+
|
|
|
+ VideoHideLastPlayBarRunnable(View view) {
|
|
|
+ this.view = view;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ view.setVisibility(INVISIBLE);
|
|
|
+ isShowLastPlayBar = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 视频控制视图消失线程
|