Skip to content

Commit f48ff61

Browse files
authored
Merge pull request #7166 from litetex/various-fixes-for-mediasession-player
Various fixes related to MediasessionManager
2 parents 93aed9f + 72dbb94 commit f48ff61

File tree

5 files changed

+212
-99
lines changed

5 files changed

+212
-99
lines changed

app/src/main/java/org/schabi/newpipe/local/feed/item/StreamItem.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import org.schabi.newpipe.extractor.stream.StreamType.LIVE_STREAM
1717
import org.schabi.newpipe.extractor.stream.StreamType.VIDEO_STREAM
1818
import org.schabi.newpipe.util.Localization
1919
import org.schabi.newpipe.util.PicassoHelper
20+
import org.schabi.newpipe.util.StreamTypeUtil
2021
import java.util.concurrent.TimeUnit
2122

2223
data class StreamItem(
@@ -58,8 +59,6 @@ data class StreamItem(
5859
viewBinding.itemVideoTitleView.text = stream.title
5960
viewBinding.itemUploaderView.text = stream.uploader
6061

61-
val isLiveStream = stream.streamType == LIVE_STREAM || stream.streamType == AUDIO_LIVE_STREAM
62-
6362
if (stream.duration > 0) {
6463
viewBinding.itemDurationView.text = Localization.getDurationString(stream.duration)
6564
viewBinding.itemDurationView.setBackgroundColor(
@@ -77,7 +76,7 @@ data class StreamItem(
7776
} else {
7877
viewBinding.itemProgressView.visibility = View.GONE
7978
}
80-
} else if (isLiveStream) {
79+
} else if (StreamTypeUtil.isLiveStream(stream.streamType)) {
8180
viewBinding.itemDurationView.setText(R.string.duration_live)
8281
viewBinding.itemDurationView.setBackgroundColor(
8382
ContextCompat.getColor(

app/src/main/java/org/schabi/newpipe/player/Player.java

Lines changed: 71 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
11
package org.schabi.newpipe.player;
22

3+
import static com.google.android.exoplayer2.Player.DISCONTINUITY_REASON_AD_INSERTION;
4+
import static com.google.android.exoplayer2.Player.DISCONTINUITY_REASON_INTERNAL;
5+
import static com.google.android.exoplayer2.Player.DISCONTINUITY_REASON_PERIOD_TRANSITION;
6+
import static com.google.android.exoplayer2.Player.DISCONTINUITY_REASON_SEEK;
7+
import static com.google.android.exoplayer2.Player.DISCONTINUITY_REASON_SEEK_ADJUSTMENT;
8+
import static com.google.android.exoplayer2.Player.DiscontinuityReason;
9+
import static com.google.android.exoplayer2.Player.EventListener;
10+
import static com.google.android.exoplayer2.Player.REPEAT_MODE_ALL;
11+
import static com.google.android.exoplayer2.Player.REPEAT_MODE_OFF;
12+
import static com.google.android.exoplayer2.Player.REPEAT_MODE_ONE;
13+
import static com.google.android.exoplayer2.Player.RepeatMode;
14+
import static org.schabi.newpipe.QueueItemMenuUtil.openPopupMenu;
15+
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
16+
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
17+
import static org.schabi.newpipe.ktx.ViewUtils.animate;
18+
import static org.schabi.newpipe.ktx.ViewUtils.animateRotation;
19+
import static org.schabi.newpipe.player.MainPlayer.ACTION_CLOSE;
20+
import static org.schabi.newpipe.player.MainPlayer.ACTION_FAST_FORWARD;
21+
import static org.schabi.newpipe.player.MainPlayer.ACTION_FAST_REWIND;
22+
import static org.schabi.newpipe.player.MainPlayer.ACTION_PLAY_NEXT;
23+
import static org.schabi.newpipe.player.MainPlayer.ACTION_PLAY_PAUSE;
24+
import static org.schabi.newpipe.player.MainPlayer.ACTION_PLAY_PREVIOUS;
25+
import static org.schabi.newpipe.player.MainPlayer.ACTION_RECREATE_NOTIFICATION;
26+
import static org.schabi.newpipe.player.MainPlayer.ACTION_REPEAT;
27+
import static org.schabi.newpipe.player.MainPlayer.ACTION_SHUFFLE;
28+
import static org.schabi.newpipe.player.helper.PlayerHelper.MinimizeMode.MINIMIZE_ON_EXIT_MODE_BACKGROUND;
29+
import static org.schabi.newpipe.player.helper.PlayerHelper.MinimizeMode.MINIMIZE_ON_EXIT_MODE_NONE;
30+
import static org.schabi.newpipe.player.helper.PlayerHelper.MinimizeMode.MINIMIZE_ON_EXIT_MODE_POPUP;
31+
import static org.schabi.newpipe.player.helper.PlayerHelper.buildCloseOverlayLayoutParams;
32+
import static org.schabi.newpipe.player.helper.PlayerHelper.formatSpeed;
33+
import static org.schabi.newpipe.player.helper.PlayerHelper.getMinimizeOnExitAction;
34+
import static org.schabi.newpipe.player.helper.PlayerHelper.getMinimumVideoHeight;
35+
import static org.schabi.newpipe.player.helper.PlayerHelper.getTimeString;
36+
import static org.schabi.newpipe.player.helper.PlayerHelper.globalScreenOrientationLocked;
37+
import static org.schabi.newpipe.player.helper.PlayerHelper.isPlaybackResumeEnabled;
38+
import static org.schabi.newpipe.player.helper.PlayerHelper.nextRepeatMode;
39+
import static org.schabi.newpipe.player.helper.PlayerHelper.nextResizeModeAndSaveToPrefs;
40+
import static org.schabi.newpipe.player.helper.PlayerHelper.retrievePlaybackParametersFromPrefs;
41+
import static org.schabi.newpipe.player.helper.PlayerHelper.retrievePlayerTypeFromIntent;
42+
import static org.schabi.newpipe.player.helper.PlayerHelper.retrievePopupLayoutParamsFromPrefs;
43+
import static org.schabi.newpipe.player.helper.PlayerHelper.retrieveSeekDurationFromPreferences;
44+
import static org.schabi.newpipe.player.helper.PlayerHelper.savePlaybackParametersToPrefs;
45+
import static org.schabi.newpipe.util.ListHelper.getPopupResolutionIndex;
46+
import static org.schabi.newpipe.util.ListHelper.getResolutionIndex;
47+
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
48+
import static org.schabi.newpipe.util.Localization.containsCaseInsensitive;
49+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
50+
351
import android.animation.Animator;
452
import android.animation.AnimatorListenerAdapter;
553
import android.animation.ObjectAnimator;
@@ -94,7 +142,6 @@
94142
import org.schabi.newpipe.extractor.MediaFormat;
95143
import org.schabi.newpipe.extractor.stream.StreamInfo;
96144
import org.schabi.newpipe.extractor.stream.StreamSegment;
97-
import org.schabi.newpipe.extractor.stream.StreamType;
98145
import org.schabi.newpipe.extractor.stream.VideoStream;
99146
import org.schabi.newpipe.fragments.OnScrollBelowItemsListener;
100147
import org.schabi.newpipe.fragments.detail.VideoDetailFragment;
@@ -127,74 +174,28 @@
127174
import org.schabi.newpipe.player.resolver.VideoPlaybackResolver;
128175
import org.schabi.newpipe.player.seekbarpreview.SeekbarPreviewThumbnailHelper;
129176
import org.schabi.newpipe.player.seekbarpreview.SeekbarPreviewThumbnailHolder;
177+
import org.schabi.newpipe.util.StreamTypeUtil;
130178
import org.schabi.newpipe.util.DeviceUtils;
131-
import org.schabi.newpipe.util.external_communication.KoreUtils;
132179
import org.schabi.newpipe.util.ListHelper;
133180
import org.schabi.newpipe.util.NavigationHelper;
134181
import org.schabi.newpipe.util.PicassoHelper;
135182
import org.schabi.newpipe.util.SerializedCache;
183+
import org.schabi.newpipe.util.external_communication.KoreUtils;
136184
import org.schabi.newpipe.util.external_communication.ShareUtils;
137185
import org.schabi.newpipe.views.ExpandableSurfaceView;
138186

139187
import java.io.IOException;
140188
import java.util.ArrayList;
141189
import java.util.List;
142190
import java.util.Objects;
191+
import java.util.Optional;
143192

144193
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
145194
import io.reactivex.rxjava3.core.Observable;
146195
import io.reactivex.rxjava3.disposables.CompositeDisposable;
147196
import io.reactivex.rxjava3.disposables.Disposable;
148197
import io.reactivex.rxjava3.disposables.SerialDisposable;
149198

150-
import static com.google.android.exoplayer2.Player.DISCONTINUITY_REASON_AD_INSERTION;
151-
import static com.google.android.exoplayer2.Player.DISCONTINUITY_REASON_INTERNAL;
152-
import static com.google.android.exoplayer2.Player.DISCONTINUITY_REASON_PERIOD_TRANSITION;
153-
import static com.google.android.exoplayer2.Player.DISCONTINUITY_REASON_SEEK;
154-
import static com.google.android.exoplayer2.Player.DISCONTINUITY_REASON_SEEK_ADJUSTMENT;
155-
import static com.google.android.exoplayer2.Player.DiscontinuityReason;
156-
import static com.google.android.exoplayer2.Player.EventListener;
157-
import static com.google.android.exoplayer2.Player.REPEAT_MODE_ALL;
158-
import static com.google.android.exoplayer2.Player.REPEAT_MODE_OFF;
159-
import static com.google.android.exoplayer2.Player.REPEAT_MODE_ONE;
160-
import static com.google.android.exoplayer2.Player.RepeatMode;
161-
import static java.util.concurrent.TimeUnit.MILLISECONDS;
162-
import static org.schabi.newpipe.QueueItemMenuUtil.openPopupMenu;
163-
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
164-
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
165-
import static org.schabi.newpipe.ktx.ViewUtils.animate;
166-
import static org.schabi.newpipe.ktx.ViewUtils.animateRotation;
167-
import static org.schabi.newpipe.player.MainPlayer.ACTION_CLOSE;
168-
import static org.schabi.newpipe.player.MainPlayer.ACTION_FAST_FORWARD;
169-
import static org.schabi.newpipe.player.MainPlayer.ACTION_FAST_REWIND;
170-
import static org.schabi.newpipe.player.MainPlayer.ACTION_PLAY_NEXT;
171-
import static org.schabi.newpipe.player.MainPlayer.ACTION_PLAY_PAUSE;
172-
import static org.schabi.newpipe.player.MainPlayer.ACTION_PLAY_PREVIOUS;
173-
import static org.schabi.newpipe.player.MainPlayer.ACTION_RECREATE_NOTIFICATION;
174-
import static org.schabi.newpipe.player.MainPlayer.ACTION_REPEAT;
175-
import static org.schabi.newpipe.player.MainPlayer.ACTION_SHUFFLE;
176-
import static org.schabi.newpipe.player.helper.PlayerHelper.MinimizeMode.MINIMIZE_ON_EXIT_MODE_BACKGROUND;
177-
import static org.schabi.newpipe.player.helper.PlayerHelper.MinimizeMode.MINIMIZE_ON_EXIT_MODE_NONE;
178-
import static org.schabi.newpipe.player.helper.PlayerHelper.MinimizeMode.MINIMIZE_ON_EXIT_MODE_POPUP;
179-
import static org.schabi.newpipe.player.helper.PlayerHelper.buildCloseOverlayLayoutParams;
180-
import static org.schabi.newpipe.player.helper.PlayerHelper.formatSpeed;
181-
import static org.schabi.newpipe.player.helper.PlayerHelper.getMinimizeOnExitAction;
182-
import static org.schabi.newpipe.player.helper.PlayerHelper.getMinimumVideoHeight;
183-
import static org.schabi.newpipe.player.helper.PlayerHelper.getTimeString;
184-
import static org.schabi.newpipe.player.helper.PlayerHelper.globalScreenOrientationLocked;
185-
import static org.schabi.newpipe.player.helper.PlayerHelper.isPlaybackResumeEnabled;
186-
import static org.schabi.newpipe.player.helper.PlayerHelper.nextRepeatMode;
187-
import static org.schabi.newpipe.player.helper.PlayerHelper.nextResizeModeAndSaveToPrefs;
188-
import static org.schabi.newpipe.player.helper.PlayerHelper.retrievePlaybackParametersFromPrefs;
189-
import static org.schabi.newpipe.player.helper.PlayerHelper.retrievePlayerTypeFromIntent;
190-
import static org.schabi.newpipe.player.helper.PlayerHelper.retrievePopupLayoutParamsFromPrefs;
191-
import static org.schabi.newpipe.player.helper.PlayerHelper.retrieveSeekDurationFromPreferences;
192-
import static org.schabi.newpipe.player.helper.PlayerHelper.savePlaybackParametersToPrefs;
193-
import static org.schabi.newpipe.util.ListHelper.getPopupResolutionIndex;
194-
import static org.schabi.newpipe.util.ListHelper.getResolutionIndex;
195-
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
196-
import static org.schabi.newpipe.util.Localization.containsCaseInsensitive;
197-
198199
public final class Player implements
199200
EventListener,
200201
PlaybackListener,
@@ -1622,12 +1623,6 @@ private void onUpdateProgress(final int currentProgress,
16221623
if (isQueueVisible) {
16231624
updateQueueTime(currentProgress);
16241625
}
1625-
1626-
final boolean showThumbnail = prefs.getBoolean(
1627-
context.getString(R.string.show_thumbnail_key), true);
1628-
// setMetadata only updates the metadata when any of the metadata keys are null
1629-
mediaSessionManager.setMetadata(getVideoTitle(), getUploaderName(),
1630-
showThumbnail ? getThumbnail() : null, duration);
16311626
}
16321627

16331628
private void startProgressLoop() {
@@ -1652,10 +1647,10 @@ private void triggerProgressUpdate() {
16521647
// TODO: revert #6307 when introducing proper HLS support
16531648
final int duration;
16541649
if (currentItem != null
1655-
&& currentItem.getStreamType() != StreamType.AUDIO_LIVE_STREAM
1656-
&& currentItem.getStreamType() != StreamType.LIVE_STREAM) {
1650+
&& !StreamTypeUtil.isLiveStream(currentItem.getStreamType())
1651+
) {
16571652
// convert seconds to milliseconds
1658-
duration = (int) (currentItem.getDuration() * 1000);
1653+
duration = (int) (currentItem.getDuration() * 1000);
16591654
} else {
16601655
duration = (int) simpleExoPlayer.getDuration();
16611656
}
@@ -2946,6 +2941,18 @@ private void onMetadataChanged(@NonNull final MediaSourceTag tag) {
29462941
tag.getMetadata().getPreviewFrames());
29472942

29482943
NotificationUtil.getInstance().createNotificationIfNeededAndUpdate(this, false);
2944+
2945+
final boolean showThumbnail = prefs.getBoolean(
2946+
context.getString(R.string.show_thumbnail_key), true);
2947+
mediaSessionManager.setMetadata(
2948+
getVideoTitle(),
2949+
getUploaderName(),
2950+
showThumbnail ? Optional.ofNullable(getThumbnail()) : Optional.empty(),
2951+
StreamTypeUtil.isLiveStream(tag.getMetadata().getStreamType())
2952+
? -1
2953+
: tag.getMetadata().getDuration()
2954+
);
2955+
29492956
notifyMetadataUpdateToListeners();
29502957

29512958
if (areSegmentsVisible) {
@@ -3023,9 +3030,11 @@ public String getUploaderName() {
30233030

30243031
@Nullable
30253032
public Bitmap getThumbnail() {
3026-
return currentThumbnail == null
3027-
? BitmapFactory.decodeResource(context.getResources(), R.drawable.dummy_thumbnail)
3028-
: currentThumbnail;
3033+
if (currentThumbnail == null) {
3034+
currentThumbnail = BitmapFactory.decodeResource(
3035+
context.getResources(), R.drawable.dummy_thumbnail);
3036+
}
3037+
return currentThumbnail;
30293038
}
30303039
//endregion
30313040

0 commit comments

Comments
 (0)