From 0567a1faf8a9bc88974285c082e8227c75ce8872 Mon Sep 17 00:00:00 2001 From: Rajeef Date: Fri, 29 Jun 2018 17:04:53 +0530 Subject: [PATCH] feat: Introduce support for Live-VOD mode for live streams --- .../ui/DefaultPlayerUIController.java | 81 ++++++++++++++++++- .../src/main/res/drawable/ic_live_dot_red.xml | 8 ++ .../main/res/drawable/ic_live_dot_white.xml | 8 ++ 3 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 core/src/main/res/drawable/ic_live_dot_red.xml create mode 100644 core/src/main/res/drawable/ic_live_dot_white.xml diff --git a/core/src/main/java/com/pierfrancescosoffritti/androidyoutubeplayer/ui/DefaultPlayerUIController.java b/core/src/main/java/com/pierfrancescosoffritti/androidyoutubeplayer/ui/DefaultPlayerUIController.java index 3331ef05..faead82b 100644 --- a/core/src/main/java/com/pierfrancescosoffritti/androidyoutubeplayer/ui/DefaultPlayerUIController.java +++ b/core/src/main/java/com/pierfrancescosoffritti/androidyoutubeplayer/ui/DefaultPlayerUIController.java @@ -71,6 +71,10 @@ public class DefaultPlayerUIController implements PlayerUIController, YouTubePla private boolean showPlayPauseButton = true; private boolean showBufferingProgress = true; + private boolean isLive = false; + private boolean isLiveVOD = false; + private float videoDurationValue = 0; + public DefaultPlayerUIController(@NonNull YouTubePlayerView youTubePlayerView, @NonNull YouTubePlayer youTubePlayer) { this.youTubePlayerView = youTubePlayerView; this.youTubePlayer = youTubePlayer; @@ -108,6 +112,7 @@ private void initViews(View controlsView) { playPauseButton.setOnClickListener(this); menuButton.setOnClickListener(this); fullScreenButton.setOnClickListener(this); + liveVideoIndicator.setOnClickListener(this); } @Override @@ -139,12 +144,15 @@ public void showPlayPauseButton(boolean show) { @Override public void enableLiveVideoUI(boolean enable) { + isLive = enable; + enableLiveVODUI(false); if(enable) { videoDuration.setVisibility(View.INVISIBLE); - seekBar.setVisibility(View.INVISIBLE); videoCurrentTime.setVisibility(View.INVISIBLE); liveVideoIndicator.setVisibility(View.VISIBLE); + liveVideoIndicator.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.ic_live_dot_red,0,0,0); + liveVideoIndicator.setCompoundDrawablePadding((int) youTubePlayerView.getContext().getResources().getDimension(R.dimen._8dp)); } else { videoDuration.setVisibility(View.VISIBLE); seekBar.setVisibility(View.VISIBLE); @@ -154,6 +162,24 @@ public void enableLiveVideoUI(boolean enable) { } } + /** + * This is the case when user manually changes the seekbar position during a live stream. + * In this case, the player switches to a Live-VOD (Video-On-Demand) mode. + * UI changes are made on 1) Live video indicator tag, 2) Seekbar behaviour. + * This is in sync with the standard behaviour inside youtube app. + * + */ + private void enableLiveVODUI(boolean enable) { + isLiveVOD = enable; + if (enable) { + liveVideoIndicator.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.ic_live_dot_white,0,0,0); + videoCurrentTime.setVisibility(View.VISIBLE); + } else { + liveVideoIndicator.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.ic_live_dot_red,0,0,0); + videoCurrentTime.setVisibility(View.INVISIBLE); + } + } + /** * Set custom action to the left of the Play/Pause button */ @@ -266,6 +292,20 @@ else if(view == fullScreenButton) onFullScreenButtonPressed(); else if(view == menuButton) onMenuButtonPressed(); + else if (view == liveVideoIndicator) + onLiveVideoIndicatorPressed(); + } + + /** + * During a live session when user switches to VOD mode by moving the seekbar, + * this allows the user to get back to the live mode instantly. + * This is in sync with the default youtube app behaviour + */ + private void onLiveVideoIndicatorPressed() { + enableLiveVideoUI(true); + if (videoDurationValue != 0) + youTubePlayer.seekTo(videoDurationValue); + seekBar.setProgress(Math.round(videoDurationValue)); } private void onMenuButtonPressed() { @@ -442,6 +482,7 @@ public void onCurrentSecond(float second) { @Override public void onVideoDuration(float duration) { + videoDurationValue = duration; videoDuration.setText(Utils.formatTime(duration)); seekBar.setMax((int) duration); } @@ -479,13 +520,45 @@ public void onClick(View view) { @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { - videoCurrentTime.setText(Utils.formatTime(i)); + if (isLive) { + handleProgressChangedDuringLive(seekBar,i); + } else { + videoCurrentTime.setText(Utils.formatTime(i)); + } + } + + /** + * This handles the case when progress changes during Live stream. + * There are two scenarios being handled here a) Live b) Live-VOD. + * a) In case of Live, we maintain the seekbar to the value same same as duration. + * b) In case of Live-VOD, the current time is updated to show the negative value of + * current time, which lets user know how far they're behind the Live stream. + * + * Both a) & b) are in sync with default youtube app behaviour. + * + */ + private void handleProgressChangedDuringLive(SeekBar seekBar, int i) { + // Can't do much without video duration value + if (videoDurationValue == 0) + return; + + if (i == videoDurationValue) { + enableLiveVideoUI(true); + return; + } + if (isLiveVOD) { + String currentTime = "-" + Utils.formatTime(videoDurationValue - i); + videoCurrentTime.setText(currentTime); + } else { + seekBar.setProgress(Math.round(videoDurationValue)); + } } @Override public void onStartTrackingTouch(SeekBar seekBar) { - seekBarTouchStarted = true; - } + if (isLive) + enableLiveVODUI(true); + seekBarTouchStarted = true; } @Override public void onStopTrackingTouch(SeekBar seekBar) { diff --git a/core/src/main/res/drawable/ic_live_dot_red.xml b/core/src/main/res/drawable/ic_live_dot_red.xml new file mode 100644 index 00000000..d8c42e52 --- /dev/null +++ b/core/src/main/res/drawable/ic_live_dot_red.xml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file diff --git a/core/src/main/res/drawable/ic_live_dot_white.xml b/core/src/main/res/drawable/ic_live_dot_white.xml new file mode 100644 index 00000000..d29ca820 --- /dev/null +++ b/core/src/main/res/drawable/ic_live_dot_white.xml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file