react-native-nitro-player 0.6.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1201,6 +1201,20 @@ class TrackPlayerCore private constructor(
1201
1201
  val currentIndex = player.currentMediaItemIndex
1202
1202
  if (currentIndex < 0) return
1203
1203
 
1204
+ // Handle removed-current-track case: if the currently playing media item is no longer
1205
+ // in currentTracks (e.g. the user removed it while it was playing), delegate to
1206
+ // playFromIndexInternal so the player immediately starts the next track.
1207
+ val currentTrackId = player.currentMediaItem?.mediaId?.let { extractTrackId(it) }
1208
+ if (currentTrackId != null && currentTracks.none { it.id == currentTrackId }) {
1209
+ val targetIndex = when {
1210
+ currentTracks.isEmpty() -> return
1211
+ currentTrackIndex < currentTracks.size -> currentTrackIndex
1212
+ else -> currentTracks.size - 1
1213
+ }
1214
+ playFromIndexInternal(targetIndex)
1215
+ return
1216
+ }
1217
+
1204
1218
  val newQueueTracks = ArrayList<TrackItem>(playNextStack.size + upNextQueue.size + currentTracks.size)
1205
1219
 
1206
1220
  // Add playNext stack (LIFO - most recently added plays first)
@@ -1609,6 +1623,35 @@ class TrackPlayerCore private constructor(
1609
1623
  if (currentPlaylistId != null && affectedPlaylists.containsKey(currentPlaylistId)) {
1610
1624
  NitroPlayerLogger.log("TrackPlayerCore") { "🔄 Rebuilding queue - ${affectedPlaylists[currentPlaylistId]} tracks updated in current playlist" }
1611
1625
 
1626
+ // PlaylistManager.updateTracks() creates a new Playlist via .copy(tracks = newTracks),
1627
+ // so our currentTracks reference still points at the old list with empty URLs.
1628
+ // Refresh it now so rebuildQueueFromCurrentPosition builds MediaItems with the
1629
+ // resolved URLs, allowing ExoPlayer to pre-buffer the next track for gapless playback.
1630
+ val refreshedPlaylist = playlistManager.getPlaylist(currentPlaylistId!!)
1631
+ if (refreshedPlaylist != null) {
1632
+ currentTracks = refreshedPlaylist.tracks
1633
+
1634
+ // Also reconcile any queued items that still reference old TrackItem instances
1635
+ // from this playlist, so that gapless pre-buffering uses tracks with resolved URLs.
1636
+ val updatedTrackById = currentTracks.associateBy { it.id }
1637
+
1638
+ // Update playNextStack entries to point at the refreshed TrackItem objects.
1639
+ playNextStack.forEachIndexed { index, track ->
1640
+ val updated = updatedTrackById[track.id]
1641
+ if (updated != null && updated !== track) {
1642
+ playNextStack[index] = updated
1643
+ }
1644
+ }
1645
+
1646
+ // Update upNextQueue entries to point at the refreshed TrackItem objects.
1647
+ upNextQueue.forEachIndexed { index, track ->
1648
+ val updated = updatedTrackById[track.id]
1649
+ if (updated != null && updated !== track) {
1650
+ upNextQueue[index] = updated
1651
+ }
1652
+ }
1653
+ }
1654
+
1612
1655
  // This method preserves current item and gapless buffering
1613
1656
  rebuildQueueFromCurrentPosition()
1614
1657
 
@@ -1931,6 +1931,21 @@ class TrackPlayerCore: NSObject {
1931
1931
  let currentItem = player.currentItem
1932
1932
  let playingItems = player.items()
1933
1933
 
1934
+ // ---- Handle removed-current-track case ----
1935
+ // If the currently playing AVPlayerItem is no longer in currentTracks (e.g. the user
1936
+ // removed it while it was playing), delegate to rebuildQueueFromPlaylistIndex so the
1937
+ // player immediately starts what is now at currentTrackIndex in the updated list.
1938
+ if let playingTrackId = currentItem?.trackId,
1939
+ !currentTracks.contains(where: { $0.id == playingTrackId }) {
1940
+ let targetIndex = currentTrackIndex < currentTracks.count
1941
+ ? currentTrackIndex
1942
+ : currentTracks.count - 1
1943
+ if targetIndex >= 0 {
1944
+ _ = rebuildQueueFromPlaylistIndex(index: targetIndex)
1945
+ }
1946
+ return
1947
+ }
1948
+
1934
1949
  // ---- Build the desired upcoming track list ----
1935
1950
 
1936
1951
  var newQueueTracks: [TrackItem] = []
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nitro-player",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "A powerful audio player library for React Native with playlist management, playback controls, and support for Android Auto and CarPlay",
5
5
  "main": "lib/index",
6
6
  "module": "lib/index",