Support pass opening and ending

pull/21/head
FongMi 4 years ago
parent 3fc7e68acc
commit e4fd6fd106
  1. 44
      app/src/leanback/java/com/fongmi/android/tv/ui/activity/DetailActivity.java
  2. 16
      app/src/main/java/com/fongmi/android/tv/player/Players.java

@ -361,11 +361,50 @@ public class DetailActivity extends BaseActivity implements KeyDown.Listener {
}
};
private final Runnable mProgress = new Runnable() {
@Override
public void run() {
if (mHistory.getOpening() >= Players.get().getCurrentPosition()) {
Players.get().seekTo(mHistory.getOpening());
}
if (mHistory.getEnding() + Players.get().getCurrentPosition() >= Players.get().getDuration()) {
onNext();
}
mHandler.postDelayed(mProgress, 1000);
}
};
@Subscribe(threadMode = ThreadMode.MAIN)
public void onPlayerEvent(PlayerEvent event) {
mBinding.progress.getRoot().setVisibility(event.getState() == Player.STATE_BUFFERING ? View.VISIBLE : View.GONE);
if (event.getState() == Player.STATE_ENDED) onNext();
Notify.show(event.getMsg());
switch (event.getState()) {
case 0:
checkPosition();
break;
case Player.STATE_BUFFERING:
mBinding.progress.getRoot().setVisibility(View.VISIBLE);
break;
case Player.STATE_READY:
mBinding.progress.getRoot().setVisibility(View.GONE);
break;
case Player.STATE_ENDED:
onNext();
break;
}
}
private void checkPosition() {
Players.get().seekTo(mHistory.getDuration());
stopTimer();
setTimer();
}
private void stopTimer() {
mHandler.removeCallbacks(mProgress);
}
private void setTimer() {
mHandler.postDelayed(mProgress, 1000);
}
@Override
@ -435,6 +474,7 @@ public class DetailActivity extends BaseActivity implements KeyDown.Listener {
@Override
protected void onDestroy() {
super.onDestroy();
stopTimer();
updateHistory();
Players.get().stop();
EventBus.getDefault().unregister(this);

@ -7,9 +7,7 @@ import androidx.annotation.NonNull;
import com.fongmi.android.tv.App;
import com.fongmi.android.tv.R;
import com.fongmi.android.tv.bean.History;
import com.fongmi.android.tv.bean.Result;
import com.fongmi.android.tv.db.AppDatabase;
import com.fongmi.android.tv.event.PlayerEvent;
import com.fongmi.android.tv.ui.custom.CustomWebView;
import com.fongmi.android.tv.utils.ResUtil;
@ -80,7 +78,7 @@ public class Players implements Player.Listener {
public String getTime(long time) {
time = getCurrentPosition() + time;
if (time > exoPlayer.getDuration()) time = exoPlayer.getDuration();
if (time > getDuration()) time = getDuration();
else if (time < 0) time = 0;
return getStringForTime(time);
}
@ -93,6 +91,10 @@ public class Players implements Player.Listener {
return exoPlayer.getCurrentPosition();
}
public long getDuration() {
return exoPlayer.getDuration();
}
public void seekTo(int time) {
exoPlayer.seekTo(getCurrentPosition() + time);
}
@ -134,19 +136,13 @@ public class Players implements Player.Listener {
handler.post(() -> {
handler.removeCallbacks(mTimer);
exoPlayer.setMediaSource(ExoUtil.getSource(headers, url));
EventBus.getDefault().post(new PlayerEvent(0));
exoPlayer.prepare();
exoPlayer.play();
checkPosition();
webView.stop();
});
}
private void checkPosition() {
History history = AppDatabase.get().getHistoryDao().find(getKey());
if (history == null) return;
seekTo(history.getDuration());
}
public void pause() {
if (exoPlayer != null) {
exoPlayer.pause();

Loading…
Cancel
Save