@@ -765,17 +765,15 @@ public void onBitmapLoaded(final Bitmap bitmap, final Picasso.LoadedFrom from) {
765765 + " -> " + bitmap .getWidth () + "x" + bitmap .getHeight () + "], from = ["
766766 + from + "]" );
767767 }
768- currentThumbnail = bitmap ;
769768 // 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 );
771770 }
772771
773772 @ Override
774773 public void onBitmapFailed (final Exception e , final Drawable errorDrawable ) {
775774 Log .e (TAG , "Thumbnail - onBitmapFailed() called" , e );
776- currentThumbnail = null ;
777775 // 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 );
779777 }
780778
781779 @ Override
@@ -798,7 +796,7 @@ private void loadCurrentThumbnail(final String url) {
798796
799797 // Unset currentThumbnail, since it is now outdated. This ensures it is not used in media
800798 // session metadata while the new thumbnail is being loaded by Picasso.
801- currentThumbnail = null ;
799+ onThumbnailLoaded ( null ) ;
802800 if (isNullOrEmpty (url )) {
803801 return ;
804802 }
@@ -813,6 +811,16 @@ private void cancelLoadingCurrentThumbnail() {
813811 // cancel the Picasso job associated with the player thumbnail, if any
814812 PicassoHelper .cancelTag (PICASSO_PLAYER_THUMBNAIL_TAG );
815813 }
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+ }
816824 //endregion
817825
818826
@@ -1501,48 +1509,50 @@ public void onPlaybackSynchronize(@NonNull final PlayQueueItem item, final boole
15011509 Log .d (TAG , "Playback - onPlaybackSynchronize(was blocked: " + wasBlocked
15021510 + ") called with item=[" + item .getTitle () + "], url=[" + item .getUrl () + "]" );
15031511 }
1504- if (exoPlayerIsNull () || playQueue == null ) {
1505- return ;
1512+ if (exoPlayerIsNull () || playQueue == null || currentItem == item ) {
1513+ return ; // nothing to synchronize
15061514 }
15071515
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 ());
15131522
1514- // If nothing to synchronize
1515- if (!hasPlayQueueItemChanged ) {
1516- return ;
1517- }
15181523 currentItem = item ;
15191524
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
15341540 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 );
15391548 }
15401549
1550+ // sync the player index with the queue index, and seek to the correct position
15411551 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 );
15441554 } else {
1545- simpleExoPlayer .seekToDefaultPosition (currentPlayQueueIndex );
1555+ simpleExoPlayer .seekToDefaultPosition (playQueueIndex );
15461556 }
15471557 }
15481558 }
0 commit comments