@@ -765,17 +765,15 @@ public void onBitmapLoaded(final Bitmap bitmap, final Picasso.LoadedFrom from) {
765
765
+ " -> " + bitmap .getWidth () + "x" + bitmap .getHeight () + "], from = ["
766
766
+ from + "]" );
767
767
}
768
- currentThumbnail = bitmap ;
769
768
// there is a new thumbnail, so e.g. the end screen thumbnail needs to change, too.
770
- UIs . call ( playerUi -> playerUi . onThumbnailLoaded (bitmap ) );
769
+ onThumbnailLoaded (bitmap );
771
770
}
772
771
773
772
@ Override
774
773
public void onBitmapFailed (final Exception e , final Drawable errorDrawable ) {
775
774
Log .e (TAG , "Thumbnail - onBitmapFailed() called" , e );
776
- currentThumbnail = null ;
777
775
// there is a new thumbnail, so e.g. the end screen thumbnail needs to change, too.
778
- UIs . call ( playerUi -> playerUi . onThumbnailLoaded (null ) );
776
+ onThumbnailLoaded (null );
779
777
}
780
778
781
779
@ Override
@@ -798,7 +796,7 @@ private void loadCurrentThumbnail(final String url) {
798
796
799
797
// Unset currentThumbnail, since it is now outdated. This ensures it is not used in media
800
798
// session metadata while the new thumbnail is being loaded by Picasso.
801
- currentThumbnail = null ;
799
+ onThumbnailLoaded ( null ) ;
802
800
if (isNullOrEmpty (url )) {
803
801
return ;
804
802
}
@@ -813,6 +811,16 @@ private void cancelLoadingCurrentThumbnail() {
813
811
// cancel the Picasso job associated with the player thumbnail, if any
814
812
PicassoHelper .cancelTag (PICASSO_PLAYER_THUMBNAIL_TAG );
815
813
}
814
+
815
+ private void onThumbnailLoaded (@ Nullable final Bitmap bitmap ) {
816
+ // Avoid useless thumbnail updates, if the thumbnail has not actually changed. Based on the
817
+ // thumbnail loading code, this if would be skipped only when both bitmaps are `null`, since
818
+ // onThumbnailLoaded won't be called twice with the same nonnull bitmap by Picasso's target.
819
+ if (currentThumbnail != bitmap ) {
820
+ currentThumbnail = bitmap ;
821
+ UIs .call (playerUi -> playerUi .onThumbnailLoaded (bitmap ));
822
+ }
823
+ }
816
824
//endregion
817
825
818
826
@@ -1501,48 +1509,50 @@ public void onPlaybackSynchronize(@NonNull final PlayQueueItem item, final boole
1501
1509
Log .d (TAG , "Playback - onPlaybackSynchronize(was blocked: " + wasBlocked
1502
1510
+ ") called with item=[" + item .getTitle () + "], url=[" + item .getUrl () + "]" );
1503
1511
}
1504
- if (exoPlayerIsNull () || playQueue == null ) {
1505
- return ;
1512
+ if (exoPlayerIsNull () || playQueue == null || currentItem == item ) {
1513
+ return ; // nothing to synchronize
1506
1514
}
1507
1515
1508
- final boolean hasPlayQueueItemChanged = currentItem != item ;
1509
-
1510
- final int currentPlayQueueIndex = playQueue .indexOf (item );
1511
- final int currentPlaylistIndex = simpleExoPlayer .getCurrentMediaItemIndex ();
1512
- final int currentPlaylistSize = simpleExoPlayer .getCurrentTimeline ().getWindowCount ();
1516
+ final int playQueueIndex = playQueue .indexOf (item );
1517
+ final int playlistIndex = simpleExoPlayer .getCurrentMediaItemIndex ();
1518
+ final int playlistSize = simpleExoPlayer .getCurrentTimeline ().getWindowCount ();
1519
+ final boolean removeThumbnailBeforeSync = currentItem == null
1520
+ || currentItem .getServiceId () != item .getServiceId ()
1521
+ || !currentItem .getUrl ().equals (item .getUrl ());
1513
1522
1514
- // If nothing to synchronize
1515
- if (!hasPlayQueueItemChanged ) {
1516
- return ;
1517
- }
1518
1523
currentItem = item ;
1519
1524
1520
- // Check if on wrong window
1521
- if (currentPlayQueueIndex != playQueue .getIndex ()) {
1522
- Log .e (TAG , "Playback - Play Queue may be desynchronized: item "
1523
- + "index=[" + currentPlayQueueIndex + "], "
1524
- + "queue index=[" + playQueue .getIndex () + "]" );
1525
-
1526
- // Check if bad seek position
1527
- } else if ((currentPlaylistSize > 0 && currentPlayQueueIndex >= currentPlaylistSize )
1528
- || currentPlayQueueIndex < 0 ) {
1529
- Log .e (TAG , "Playback - Trying to seek to invalid "
1530
- + "index=[" + currentPlayQueueIndex + "] with "
1531
- + "playlist length=[" + currentPlaylistSize + "]" );
1532
-
1533
- } else if (wasBlocked || currentPlaylistIndex != currentPlayQueueIndex || !isPlaying ()) {
1525
+ if (playQueueIndex != playQueue .getIndex ()) {
1526
+ // wrong window (this should be impossible, as this method is called with
1527
+ // `item=playQueue.getItem()`, so the index of that item must be equal to `getIndex()`)
1528
+ Log .e (TAG , "Playback - Play Queue may be not in sync: item index=["
1529
+ + playQueueIndex + "], " + "queue index=[" + playQueue .getIndex () + "]" );
1530
+
1531
+ } else if ((playlistSize > 0 && playQueueIndex >= playlistSize ) || playQueueIndex < 0 ) {
1532
+ // the queue and the player's timeline are not in sync, since the play queue index
1533
+ // points outside of the timeline
1534
+ Log .e (TAG , "Playback - Trying to seek to invalid index=[" + playQueueIndex
1535
+ + "] with playlist length=[" + playlistSize + "]" );
1536
+
1537
+ } else if (wasBlocked || playlistIndex != playQueueIndex || !isPlaying ()) {
1538
+ // either the player needs to be unblocked, or the play queue index has just been
1539
+ // changed and needs to be synchronized, or the player is not playing
1534
1540
if (DEBUG ) {
1535
- Log .d (TAG , "Playback - Rewinding to correct "
1536
- + "index=[" + currentPlayQueueIndex + "], "
1537
- + "from=[" + currentPlaylistIndex + "], "
1538
- + "size=[" + currentPlaylistSize + "]." );
1541
+ Log .d (TAG , "Playback - Rewinding to correct index=[" + playQueueIndex + "], "
1542
+ + "from=[" + playlistIndex + "], size=[" + playlistSize + "]." );
1543
+ }
1544
+
1545
+ if (removeThumbnailBeforeSync ) {
1546
+ // unset the current (now outdated) thumbnail to ensure it is not used during sync
1547
+ onThumbnailLoaded (null );
1539
1548
}
1540
1549
1550
+ // sync the player index with the queue index, and seek to the correct position
1541
1551
if (item .getRecoveryPosition () != PlayQueueItem .RECOVERY_UNSET ) {
1542
- simpleExoPlayer .seekTo (currentPlayQueueIndex , item .getRecoveryPosition ());
1543
- playQueue .unsetRecovery (currentPlayQueueIndex );
1552
+ simpleExoPlayer .seekTo (playQueueIndex , item .getRecoveryPosition ());
1553
+ playQueue .unsetRecovery (playQueueIndex );
1544
1554
} else {
1545
- simpleExoPlayer .seekToDefaultPosition (currentPlayQueueIndex );
1555
+ simpleExoPlayer .seekToDefaultPosition (playQueueIndex );
1546
1556
}
1547
1557
}
1548
1558
}
0 commit comments