@@ -1509,48 +1509,50 @@ public void onPlaybackSynchronize(@NonNull final PlayQueueItem item, final boole
15091509 Log .d (TAG , "Playback - onPlaybackSynchronize(was blocked: " + wasBlocked
15101510 + ") called with item=[" + item .getTitle () + "], url=[" + item .getUrl () + "]" );
15111511 }
1512- if (exoPlayerIsNull () || playQueue == null ) {
1513- return ;
1512+ if (exoPlayerIsNull () || playQueue == null || currentItem == item ) {
1513+ return ; // nothing to synchronize
15141514 }
15151515
1516- final boolean hasPlayQueueItemChanged = currentItem != item ;
1517-
1518- final int currentPlayQueueIndex = playQueue .indexOf (item );
1519- final int currentPlaylistIndex = simpleExoPlayer .getCurrentMediaItemIndex ();
1520- 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 ());
15211522
1522- // If nothing to synchronize
1523- if (!hasPlayQueueItemChanged ) {
1524- return ;
1525- }
15261523 currentItem = item ;
15271524
1528- // Check if on wrong window
1529- if (currentPlayQueueIndex != playQueue .getIndex ()) {
1530- Log .e (TAG , "Playback - Play Queue may be desynchronized: item "
1531- + "index=[" + currentPlayQueueIndex + "], "
1532- + "queue index=[" + playQueue .getIndex () + "]" );
1533-
1534- // Check if bad seek position
1535- } else if ((currentPlaylistSize > 0 && currentPlayQueueIndex >= currentPlaylistSize )
1536- || currentPlayQueueIndex < 0 ) {
1537- Log .e (TAG , "Playback - Trying to seek to invalid "
1538- + "index=[" + currentPlayQueueIndex + "] with "
1539- + "playlist length=[" + currentPlaylistSize + "]" );
1540-
1541- } 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
15421540 if (DEBUG ) {
1543- Log .d (TAG , "Playback - Rewinding to correct "
1544- + "index=[" + currentPlayQueueIndex + "], "
1545- + "from=[" + currentPlaylistIndex + "], "
1546- + "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 );
15471548 }
15481549
1550+ // sync the player index with the queue index, and seek to the correct position
15491551 if (item .getRecoveryPosition () != PlayQueueItem .RECOVERY_UNSET ) {
1550- simpleExoPlayer .seekTo (currentPlayQueueIndex , item .getRecoveryPosition ());
1551- playQueue .unsetRecovery (currentPlayQueueIndex );
1552+ simpleExoPlayer .seekTo (playQueueIndex , item .getRecoveryPosition ());
1553+ playQueue .unsetRecovery (playQueueIndex );
15521554 } else {
1553- simpleExoPlayer .seekToDefaultPosition (currentPlayQueueIndex );
1555+ simpleExoPlayer .seekToDefaultPosition (playQueueIndex );
15541556 }
15551557 }
15561558 }
0 commit comments