Autoplay enhancement and new button at the top left corner

- added a video close button to the top left corner
- autoplay will not work if stream plays in background or popup players
This commit is contained in:
Avently 2020-01-09 18:28:06 +03:00
parent 4c57893312
commit 22bb129bd9
5 changed files with 65 additions and 13 deletions

View File

@ -38,9 +38,9 @@
<service
android:name=".player.BackgroundPlayer"
android:exported="false">
<intent-filter>
<!--<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</intent-filter>-->
</service>
<service

View File

@ -27,7 +27,6 @@ import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
@ -111,6 +110,7 @@ public class VideoDetailFragment
private static final float MAX_OVERLAY_ALPHA = 0.9f;
public static final String ACTION_SHOW_MAIN_PLAYER = "org.schabi.newpipe.fragments.VideoDetailFragment.ACTION_SHOW_MAIN_PLAYER";
public static final String ACTION_HIDE_MAIN_PLAYER = "org.schabi.newpipe.fragments.VideoDetailFragment.ACTION_HIDE_MAIN_PLAYER";
private boolean autoPlayEnabled;
private boolean showRelatedStreams;
@ -549,7 +549,6 @@ public class VideoDetailFragment
setOverlayPlayPauseImage();
break;
case R.id.overlay_close_button:
cleanUp();
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
break;
}
@ -874,6 +873,14 @@ public class VideoDetailFragment
return true;
}
StackItem currentPeek = stack.peek();
if (currentPeek != null && currentPeek.getPlayQueue() != playQueue) {
// When user selected a stream but didn't start playback this stream will not be added to backStack.
// Then he press Back and the last saved item from history will show up
setupFromHistoryItem(currentPeek);
return true;
}
// If we have something in history of played items we replay it here
if (player != null && player.getPlayQueue() != null && player.getPlayQueue().previous()) {
return true;
@ -888,18 +895,20 @@ public class VideoDetailFragment
// Remove top
stack.pop();
// Get stack item from the new top
StackItem peek = stack.peek();
setupFromHistoryItem(stack.peek());
return true;
}
private void setupFromHistoryItem(StackItem item) {
hideMainPlayer();
setAutoplay(false);
selectAndLoadVideo(
peek.getServiceId(),
peek.getUrl(),
!TextUtils.isEmpty(peek.getTitle()) ? peek.getTitle() : "",
peek.getPlayQueue());
return true;
item.getServiceId(),
item.getUrl(),
!TextUtils.isEmpty(item.getTitle()) ? item.getTitle() : "",
item.getPlayQueue());
}
/*//////////////////////////////////////////////////////////////////////////
@ -1165,6 +1174,7 @@ public class VideoDetailFragment
return playQueue != null && playQueue.getStreams().size() != 0
&& autoPlayEnabled
&& !isExternalPlayerEnabled()
&& (player == null || player.videoPlayerSelected())
&& isAutoplayAllowedByUser();
}
@ -1289,10 +1299,14 @@ public class VideoDetailFragment
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(ACTION_SHOW_MAIN_PLAYER)) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
} else if(intent.getAction().equals(ACTION_HIDE_MAIN_PLAYER)) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
}
};
IntentFilter intentFilter = new IntentFilter(ACTION_SHOW_MAIN_PLAYER);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_SHOW_MAIN_PLAYER);
intentFilter.addAction(ACTION_HIDE_MAIN_PLAYER);
activity.registerReceiver(broadcastReceiver, intentFilter);
}
@ -1662,7 +1676,8 @@ public class VideoDetailFragment
public void onProgressUpdate(int currentProgress, int duration, int bufferPercent) {
// Progress updates every second even if media is paused. It's useless until playing
if (!player.getPlayer().isPlaying() || playQueue == null) return;
showPlaybackProgress(currentProgress, duration);
if (playQueue == player.getPlayQueue()) showPlaybackProgress(currentProgress, duration);
// We don't want to interrupt playback and don't want to see notification if player is stopped
// since next lines of code will enable background playback if needed

View File

@ -108,6 +108,7 @@ public class VideoPlayerImpl extends VideoPlayer
private ImageButton playWithKodi;
private ImageButton openInBrowser;
private ImageButton fullscreenButton;
private ImageButton playerCloseButton;
private ImageButton playPauseButton;
private ImageButton playPreviousButton;
@ -238,6 +239,7 @@ public class VideoPlayerImpl extends VideoPlayer
this.playWithKodi = rootView.findViewById(R.id.playWithKodi);
this.openInBrowser = rootView.findViewById(R.id.openInBrowser);
this.fullscreenButton = rootView.findViewById(R.id.fullScreenButton);
this.playerCloseButton = rootView.findViewById(R.id.playerCloseButton);
this.playPauseButton = rootView.findViewById(R.id.playPauseButton);
this.playPreviousButton = rootView.findViewById(R.id.playPreviousButton);
@ -293,6 +295,7 @@ public class VideoPlayerImpl extends VideoPlayer
shareButton.setVisibility(View.GONE);
playWithKodi.setVisibility(View.GONE);
openInBrowser.setVisibility(View.GONE);
playerCloseButton.setVisibility(View.GONE);
} else {
fullscreenButton.setVisibility(View.GONE);
getRootView().findViewById(R.id.spaceBeforeControls).setVisibility(View.VISIBLE);
@ -307,6 +310,7 @@ public class VideoPlayerImpl extends VideoPlayer
playWithKodi.setVisibility(
defaultPreferences.getBoolean(service.getString(R.string.show_play_with_kodi_key), false) ? View.VISIBLE : View.GONE);
openInBrowser.setVisibility(View.VISIBLE);
playerCloseButton.setVisibility(isFullscreen ? View.GONE : View.VISIBLE);
}
if (!isInFullscreen()) {
titleTextView.setVisibility(View.GONE);
@ -341,6 +345,7 @@ public class VideoPlayerImpl extends VideoPlayer
fullscreenButton.setOnClickListener(this);
playWithKodi.setOnClickListener(this);
openInBrowser.setOnClickListener(this);
playerCloseButton.setOnClickListener(this);
getRootView().addOnLayoutChangeListener((view, l, t, r, b, ol, ot, or, ob) -> {
if (l != ol || t != ot || r != or || b != ob) {
@ -555,9 +560,11 @@ public class VideoPlayerImpl extends VideoPlayer
if (!isInFullscreen()) {
titleTextView.setVisibility(View.GONE);
channelTextView.setVisibility(View.GONE);
playerCloseButton.setVisibility(videoPlayerSelected() ? View.VISIBLE : View.GONE);
} else {
titleTextView.setVisibility(View.VISIBLE);
channelTextView.setVisibility(View.VISIBLE);
playerCloseButton.setVisibility(View.GONE);
}
}
@ -597,6 +604,8 @@ public class VideoPlayerImpl extends VideoPlayer
} else if (v.getId() == fullscreenButton.getId()) {
toggleFullscreen();
} else if (v.getId() == playerCloseButton.getId()) {
service.sendBroadcast(new Intent(VideoDetailFragment.ACTION_HIDE_MAIN_PLAYER));
}
if (getCurrentState() != STATE_COMPLETED) {

View File

@ -175,6 +175,20 @@
android:paddingLeft="2dp"
tools:ignore="RtlHardcoded">
<ImageButton
android:id="@+id/playerCloseButton"
android:layout_width="36dp"
android:layout_height="36dp"
android:padding="6dp"
android:layout_marginEnd="8dp"
android:clickable="true"
android:focusable="true"
android:scaleType="fitXY"
android:src="?attr/close"
android:background="?attr/selectableItemBackgroundBorderless"
tools:ignore="ContentDescription,RtlHardcoded"
android:visibility="gone" />
<LinearLayout
android:id="@+id/metadataView"
android:layout_width="0dp"

View File

@ -173,6 +173,20 @@
android:paddingLeft="2dp"
tools:ignore="RtlHardcoded">
<ImageButton
android:id="@+id/playerCloseButton"
android:layout_width="36dp"
android:layout_height="36dp"
android:padding="6dp"
android:layout_marginEnd="8dp"
android:clickable="true"
android:focusable="true"
android:scaleType="fitXY"
android:src="?attr/close"
android:background="?attr/selectableItemBackgroundBorderless"
tools:ignore="ContentDescription,RtlHardcoded"
android:visibility="gone" />
<LinearLayout
android:id="@+id/metadataView"
android:layout_width="0dp"