react-native-track-player icon indicating copy to clipboard operation
react-native-track-player copied to clipboard

Replace current playing track in queue

Open mpivchev opened this issue 1 year ago • 3 comments

Discussed in https://github.com/doublesymmetry/react-native-track-player/discussions/1628

Originally posted by russriser July 31, 2022 Hiya, I'm working on a problem and would like to know the best way to go about it, as I can think of several ways to do what I want.

I have loaded a playlist of tracks into the queue that should play one-after-the-other if not interrupted by the user. However each track has multiple variants that the user should be able to switch to. If a user starts a track, say a "Guided Meditation," and then selects a track variant e.g. "with music" I believe I need to replace the currently playing audio track in the queue with the new variant, then start playing that new track.

I've tried the following

      await TrackPlayer.remove(trackIndex);
      await TrackPlayer.add(
        {
          url: trackVariant.url,
          title: currentTrack.title,
          album: currentPlaylist.title,
        },
        trackIndex
      );
      await TrackPlayer.play();

However the audio doesn't actually switch, it continues playing the old variant. I've tried adding a skipToPrevious() followed by a skipToNext() but this didn't work either.

Do I need to do a reset() in order for this to work? I am trying to avoid this.

  </div>

mpivchev avatar Aug 02 '22 15:08 mpivchev

Hi, thanks for making the issue. I didn't open one because the root of my problem seems to be intended functionality, as stated here: #1442

I am wondering how other people might be getting around the limitation.

If there is something that is issue-worthy, it's this:

You can't replace the "currently playing track" even after using TrackPlayer.stop ...since the current track index remains in place and remove doesn't remove anything in that index.

I was able to work around the issue, but I had to run stop, skipToPrevious, remove(index + 1), add(track, index + 1), skipToNext, play ....in that order

I presume that a reset would also do the trick, but that's annoying as I'd have to rebuild the queue track objects from the data fetched from my server

russriser avatar Aug 02 '22 15:08 russriser

Hi,

Just a thought; Could you add(track, currentTrackIndex + 1), skipToNext so the user will hear the new track added, and then run remove(indexToBeRemoved) while the other track already started to play?

That said, remove should perhaps work on the current index as well, or it should be documented that remove indeed does not work for the current index if that's the case?

martinmidtsund avatar Aug 02 '22 16:08 martinmidtsund

I agree with @martinmidtsund . It's not clear to me what the library should handle natively, when I think about it handling this natively effectively would just advance the queue position which is the same thing as calling skip, so I'm not sure how valuable it is. So I'm thinking documentation is best.

@dcvz, how do you want to handle an attempt to remove the currently playing track from the queue?

jspizziri avatar Aug 04 '22 09:08 jspizziri

Looks like there's a good workaround for this, so I'm closing it.

jspizziri avatar Aug 23 '22 17:08 jspizziri

@jspizziri and what is the workaround? I just also ran into this problem

pke avatar Jun 16 '23 20:06 pke