react-native-audio-browser 0.2.0-next.51 → 0.2.0-next.53

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.
@@ -158,7 +158,11 @@ internal class RemoteSwitchingPlayer(
158
158
  setPlayer(remote.receiver)
159
159
  }
160
160
 
161
- /** Lets go of [remote]'s receiver: its load is cut short and the receiver stopped. */
161
+ /**
162
+ * Lets go of [remote]'s receiver: its load is cut short and the receiver stopped. Call it after
163
+ * [setPlayer] has moved off the receiver: removing a listener delivers its pending events, and
164
+ * the stop's would show the receiver's own state.
165
+ */
162
166
  private fun leave(remote: Route.Remote) {
163
167
  cancelLoad(remote)
164
168
  remote.receiver.removeListener(receiverListener)
@@ -213,6 +217,7 @@ internal class RemoteSwitchingPlayer(
213
217
  shuffle = local.shuffleModeEnabled
214
218
  val prepared = local.playbackState != STATE_IDLE && items.isNotEmpty()
215
219
  val localPositionMs = local.currentPosition
220
+ val live = local.isCurrentMediaItemLive
216
221
  val remote = Route.Remote(target, target.player)
217
222
  enter(remote, Load.Idle(localPositionMs))
218
223
  local.stop()
@@ -220,7 +225,7 @@ internal class RemoteSwitchingPlayer(
220
225
  if (prepared) {
221
226
  resolved
222
227
  ?.onSuccess { send(remote, it, if (it.isRemoteLive) C.TIME_UNSET else localPositionMs) }
223
- ?.onFailure { fail(remote, it, localPositionMs) }
228
+ ?.onFailure { fail(remote, it, if (live) C.TIME_UNSET else localPositionMs) }
224
229
  }
225
230
  invalidateState()
226
231
  }
@@ -229,9 +234,9 @@ internal class RemoteSwitchingPlayer(
229
234
  private fun moveTo(from: Route.Remote, to: RemotePlayback) {
230
235
  val positionMs = positionOn(from)
231
236
  val load = from.load
232
- leave(from)
233
237
  val remote = Route.Remote(to, to.player)
234
238
  enter(remote, if (load is Load.Failed) load else Load.Idle(positionMs))
239
+ leave(from)
235
240
  if (load is Load.Resolving || load is Load.Sent || load is Load.Playing)
236
241
  load(remote, positionMs)
237
242
  invalidateState()
@@ -239,8 +244,8 @@ internal class RemoteSwitchingPlayer(
239
244
 
240
245
  private fun handOverToLocal(from: Route.Remote) {
241
246
  val positionMs = positionOn(from)
242
- val prepared = from.load.let { it is Load.Resolving || it is Load.Sent || it is Load.Playing }
243
- leave(from)
247
+ // A failure is the receiver's: the phone still plays the item.
248
+ val prepared = from.load !is Load.Idle && from.load !is Load.Ended
244
249
  local.repeatMode = repeat
245
250
  local.shuffleModeEnabled = shuffle
246
251
  if (queue.isEmpty) local.clearMediaItems()
@@ -249,6 +254,7 @@ internal class RemoteSwitchingPlayer(
249
254
  if (prepared && !queue.isEmpty) local.prepare()
250
255
  route = Route.Local
251
256
  setPlayer(local)
257
+ leave(from)
252
258
  }
253
259
 
254
260
  /** Where the current item is on [remote]'s receiver, for the next player to go on from. */
@@ -13,7 +13,7 @@ import com.margelo.nitro.audiobrowser.TrackStyle
13
13
  * The single Track → Media3 [MediaItem] conversion. Owns the two easy-to-drift fallbacks: the
14
14
  * displayed artwork is the transformed `artworkSource.uri` falling back to the raw `artwork` field,
15
15
  * and the mediaId is the stable `id` for playable tracks, falling back to `path` then `src` (a
16
- * Track must have one of the latter two — see the `src` vs `path` note in CONTEXT.md). The list
16
+ * Track must have one of the latter two — see the `src` vs `path` note in GLOSSARY.md). The list
17
17
  * line renders from `subtitle` (the now-playing line is re-stamped from `artist` by the now-playing
18
18
  * pipeline; see the Now Playing Metadata guide).
19
19
  */
@@ -15,6 +15,7 @@ import java.time.Duration
15
15
  import org.junit.After
16
16
  import org.junit.Assert.assertEquals
17
17
  import org.junit.Assert.assertFalse
18
+ import org.junit.Assert.assertNotEquals
18
19
  import org.junit.Assert.assertTrue
19
20
  import org.junit.Assert.fail
20
21
  import org.junit.Before
@@ -85,6 +86,19 @@ class PlayerCastTest {
85
86
  assertEquals(MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
86
87
  }
87
88
 
89
+ @Test
90
+ fun `stopping a cast after the receiver failed plays on the phone`() {
91
+ remote.fail()
92
+ idle()
93
+ assertEquals(PlaybackState.ERROR, player.playbackState)
94
+
95
+ remote.endSession()
96
+
97
+ idleUntil("the phone playing") { player.exoPlayer.playbackState != MediaPlayer.STATE_IDLE }
98
+ assertTrue(player.exoPlayer.playWhenReady)
99
+ assertNotEquals(PlaybackState.ERROR, player.playbackState)
100
+ }
101
+
88
102
  @Test
89
103
  fun `a new queue while remote plays its start item on the receiver`() {
90
104
  player.setQueue(
@@ -12,12 +12,14 @@ import com.audiobrowser.sonos.SonosPosition
12
12
  import com.audiobrowser.sonos.SonosRemote
13
13
  import com.audiobrowser.sonos.SonosRoom
14
14
  import com.audiobrowser.util.TrackFactory
15
+ import com.margelo.nitro.audiobrowser.PlaybackState
15
16
  import com.margelo.nitro.audiobrowser.RemoteOutputsOptions
16
17
  import com.margelo.nitro.audiobrowser.TimedMetadata
17
18
  import java.time.Duration
18
19
  import java.util.concurrent.CopyOnWriteArrayList
19
20
  import org.junit.After
20
21
  import org.junit.Assert.assertEquals
22
+ import org.junit.Assert.assertNotEquals
21
23
  import org.junit.Assert.fail
22
24
  import org.junit.Before
23
25
  import org.junit.Test
@@ -45,9 +47,12 @@ class PlayerSonosTest {
45
47
  artworkUri: String?,
46
48
  live: Boolean,
47
49
  ) {
50
+ check(!refuses) { "refused" }
48
51
  calls += "setUri $streamUrl"
49
52
  }
50
53
 
54
+ @Volatile var refuses = false
55
+
51
56
  override fun play() {
52
57
  calls += "play"
53
58
  }
@@ -125,9 +130,22 @@ class PlayerSonosTest {
125
130
 
126
131
  player.playWhenReady = true
127
132
 
128
- idleUntil("the start timed out", steps = 500) {
129
- player.playbackState == com.margelo.nitro.audiobrowser.PlaybackState.ERROR
130
- }
133
+ idleUntil("the start timed out", steps = 500) { player.playbackState == PlaybackState.ERROR }
134
+ }
135
+
136
+ @Test
137
+ fun `unselecting a room that refused the item plays on the phone`() {
138
+ room.refuses = true
139
+ player.sessionPlayer.setMediaItem(
140
+ TrackFactory.toMedia3(TestFixtures.track(title = "b", src = "https://s/b.mp3"))
141
+ )
142
+ idleUntil("the refusal") { player.playbackState == PlaybackState.ERROR }
143
+
144
+ SonosOutputs.unselect(ROOM)
145
+
146
+ idleUntil("the phone playing") { player.exoPlayer.playbackState != MediaPlayer.STATE_IDLE }
147
+ assertEquals(true, player.exoPlayer.playWhenReady)
148
+ assertNotEquals(PlaybackState.ERROR, player.playbackState)
131
149
  }
132
150
 
133
151
  @Test
@@ -29,7 +29,12 @@ internal class FakeLocalPlayer(looper: Looper) : SimpleBasePlayer(looper) {
29
29
  State.Builder()
30
30
  .setAvailableCommands(MediaPlayer.Commands.Builder().addAllCommands().build())
31
31
  .setPlaylist(
32
- items.mapIndexed { i, item -> MediaItemData.Builder(uids[i]).setMediaItem(item).build() }
32
+ items.mapIndexed { i, item ->
33
+ MediaItemData.Builder(uids[i])
34
+ .setMediaItem(item)
35
+ .setLiveConfiguration(item.liveConfiguration.takeIf { item.isRemoteLive })
36
+ .build()
37
+ }
33
38
  )
34
39
  .setCurrentMediaItemIndex(if (items.isEmpty()) C.INDEX_UNSET else index)
35
40
  .setContentPositionMs(positionMs)
@@ -149,6 +149,113 @@ class RemoteSwitchingPlayerTest {
149
149
  assertEquals(1, receiver.stops)
150
150
  }
151
151
 
152
+ @Test
153
+ fun `the session ending never shows the receiver's own item`() {
154
+ playLocally(listOf("a"))
155
+ remote.startSession()
156
+ idle()
157
+ val shown = mutableListOf<String?>()
158
+ player.addListener(
159
+ object : MediaPlayer.Listener {
160
+ override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
161
+ shown += mediaItem?.localConfiguration?.uri?.toString()
162
+ }
163
+ }
164
+ )
165
+
166
+ remote.endSession()
167
+ idle()
168
+
169
+ assertEquals(emptyList<String>(), shown.filterNot { it == "https://s/a" })
170
+ }
171
+
172
+ @Test
173
+ fun `the session ending after the receiver failed plays the item on the phone where it was`() {
174
+ playLocally(listOf("a", "b"), index = 1)
175
+ remote.startSession()
176
+ idle()
177
+ remote.player.positionMs = 7_000
178
+ remote.player.update()
179
+ remote.fail()
180
+ idle()
181
+ assertEquals(PlaybackException.ERROR_CODE_REMOTE_ERROR, player.playerError?.errorCode)
182
+
183
+ remote.endSession()
184
+ idle()
185
+
186
+ assertFalse(player.isRemote)
187
+ assertEquals(1, local.index)
188
+ assertEquals(7_000, local.positionMs)
189
+ assertTrue(local.playWhenReady)
190
+ assertEquals(2, local.prepares)
191
+ assertNull(player.playerError)
192
+ }
193
+
194
+ @Test
195
+ fun `the session ending after the item didn't resolve plays it on the phone`() {
196
+ unresolvable += "a"
197
+ playLocally(listOf("a"), positionMs = 4_000)
198
+ remote.startSession()
199
+ idle()
200
+ assertEquals(PlaybackException.ERROR_CODE_REMOTE_ERROR, player.playerError?.errorCode)
201
+
202
+ remote.endSession()
203
+ idle()
204
+
205
+ assertEquals(4_000, local.positionMs)
206
+ assertTrue(local.playWhenReady)
207
+ assertEquals(2, local.prepares)
208
+ }
209
+
210
+ @Test
211
+ fun `the session ending after a live item didn't resolve plays it on the phone at its live edge`() {
212
+ unresolvable += "a"
213
+ local.setMediaItems(listOf(item("a").buildUpon().setLiveConfiguration(LIVE).build()), 0, 90_000)
214
+ local.playWhenReady = true
215
+ local.prepare()
216
+ idle()
217
+ remote.startSession()
218
+ idle()
219
+
220
+ remote.endSession()
221
+ idle()
222
+
223
+ assertEquals(0, local.positionMs)
224
+ assertEquals(2, local.prepares)
225
+ }
226
+
227
+ @Test
228
+ fun `the session ending after the receiver didn't start in time plays the item on the phone`() {
229
+ playLocally(listOf("a"))
230
+ remote.startSession()
231
+ idle()
232
+ remote.player.lagging = true
233
+ player.seekToDefaultPosition()
234
+ idle()
235
+ shadowOf(looper).idleFor(Duration.ofSeconds(31))
236
+ assertEquals(PlaybackException.ERROR_CODE_REMOTE_ERROR, player.playerError?.errorCode)
237
+
238
+ remote.endSession()
239
+ idle()
240
+
241
+ assertTrue(local.playWhenReady)
242
+ assertEquals(2, local.prepares)
243
+ }
244
+
245
+ @Test
246
+ fun `the session ending after a stop leaves the phone unprepared`() {
247
+ playLocally(listOf("a"))
248
+ remote.startSession()
249
+ idle()
250
+ player.stop()
251
+ idle()
252
+
253
+ remote.endSession()
254
+ idle()
255
+
256
+ assertEquals(1, local.prepares)
257
+ }
258
+
152
259
  @Test
153
260
  fun `the receiver's end plays the next item from its start`() {
154
261
  playLocally(listOf("a", "b"), positionMs = 4_000)
@@ -812,4 +919,9 @@ class RemoteSwitchingPlayerTest {
812
919
  androidx.media3.common.MediaMetadata.Builder().setTitle(title).build()
813
920
 
814
921
  private fun idle() = shadowOf(looper).idle()
922
+
923
+ private companion object {
924
+ val LIVE: MediaItem.LiveConfiguration =
925
+ MediaItem.LiveConfiguration.Builder().setTargetOffsetMs(5_000).build()
926
+ }
815
927
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-audio-browser",
3
- "version": "0.2.0-next.51",
3
+ "version": "0.2.0-next.53",
4
4
  "description": "react-native-audio-browser is a react native package built with Nitro",
5
5
  "keywords": [
6
6
  "react-native",
@@ -71,7 +71,8 @@
71
71
  "lint": "oxlint --fix",
72
72
  "release": "semantic-release",
73
73
  "test": "vitest run",
74
- "types": "tsc --noEmit"
74
+ "types": "tsc --noEmit",
75
+ "vocab-check": "node scripts/vocab-check.ts"
75
76
  },
76
77
  "devDependencies": {
77
78
  "@react-native-community/cli": "15.0.0-alpha.2",