react-native-audio-browser 0.2.0-next.88 → 0.2.0-next.89

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.
@@ -8,6 +8,7 @@ import androidx.media3.common.Metadata
8
8
  import androidx.media3.common.PlaybackException
9
9
  import androidx.media3.common.Player as MediaPlayer
10
10
  import androidx.media3.common.SimpleBasePlayer
11
+ import androidx.media3.common.Timeline
11
12
  import com.google.common.util.concurrent.Futures
12
13
  import com.google.common.util.concurrent.ListenableFuture
13
14
  import kotlin.math.roundToInt
@@ -458,7 +459,7 @@ internal class RemoteSwitchingPlayer(
458
459
 
459
460
  override fun getState(): State {
460
461
  val state = super.getState()
461
- val remote = remoteRoute ?: return withEachBlockOnce(state)
462
+ val remote = remoteRoute ?: return withEachBlockOnce(seekableWhilePreparing(state))
462
463
  val load = remote.load
463
464
  val holds = load is Load.Playing && receiverHolds(remote, load.item)
464
465
  // Duration, seekability and liveness come from the receiver's data for the item it plays.
@@ -507,6 +508,18 @@ internal class RemoteSwitchingPlayer(
507
508
  .build()
508
509
  }
509
510
 
511
+ /**
512
+ * The local player withholds seeking from an item until it has prepared, yet keeps a seek made
513
+ * meanwhile; without the command, [seekTo] here would drop it.
514
+ */
515
+ private fun seekableWhilePreparing(state: State): State {
516
+ val index = state.currentMediaItemIndex
517
+ if (index !in 0 until state.timeline.windowCount) return state
518
+ if (!state.timeline.getWindow(index, Timeline.Window()).isPlaceholder) return state
519
+ val commands = state.availableCommands.buildUpon().add(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM)
520
+ return state.buildUpon().setAvailableCommands(commands.build()).build()
521
+ }
522
+
510
523
  private fun position(positionMs: Long) = if (positionMs == C.TIME_UNSET) 0 else positionMs
511
524
 
512
525
  /**
@@ -0,0 +1,105 @@
1
+ package com.audiobrowser.player
2
+
3
+ import android.net.Uri
4
+ import android.os.Looper
5
+ import androidx.media3.common.Player as MediaPlayer
6
+ import com.audiobrowser.TestFixtures
7
+ import com.audiobrowser.model.PlayerSetupOptions
8
+ import com.audiobrowser.remote.FakeRemote
9
+ import com.audiobrowser.silentWavBytes
10
+ import com.audiobrowser.silentWavUri
11
+ import com.margelo.nitro.audiobrowser.RemoteOutputsOptions
12
+ import java.time.Duration
13
+ import java.util.concurrent.TimeUnit
14
+ import org.junit.After
15
+ import org.junit.Assert.assertEquals
16
+ import org.junit.Assert.assertFalse
17
+ import org.junit.Assert.fail
18
+ import org.junit.Before
19
+ import org.junit.Rule
20
+ import org.junit.Test
21
+ import org.junit.rules.TemporaryFolder
22
+ import org.junit.runner.RunWith
23
+ import org.robolectric.RobolectricTestRunner
24
+ import org.robolectric.RuntimeEnvironment
25
+ import org.robolectric.Shadows.shadowOf
26
+ import org.robolectric.annotation.Config
27
+ import org.robolectric.shadows.ShadowSystemClock
28
+
29
+ /** With a remote output configured but no session, a seek right after a load lands on the phone. */
30
+ @RunWith(RobolectricTestRunner::class)
31
+ @Config(sdk = [28])
32
+ class PlayerRemoteOutputsSeekTest {
33
+ @get:Rule val folder = TemporaryFolder()
34
+
35
+ private val looper = Looper.getMainLooper()
36
+ private val player =
37
+ Player(
38
+ RuntimeEnvironment.getApplication(),
39
+ castRemote = { _, _ -> FakeRemote(looper) },
40
+ sonosRemote = { _, _ -> FakeRemote(looper) },
41
+ )
42
+
43
+ @Before
44
+ fun setUp() {
45
+ player.setup(
46
+ PlayerSetupOptions(
47
+ remoteOutputs =
48
+ RemoteOutputsOptions(cast = true, castReceiverApplicationId = null, sonos = true)
49
+ )
50
+ )
51
+ assertFalse(player.sessionPlayer === player.exoPlayer)
52
+ }
53
+
54
+ @After fun release() = player.destroy()
55
+
56
+ private fun idleUntilReady() {
57
+ repeat(200) {
58
+ if (player.exoPlayer.playbackState == MediaPlayer.STATE_READY) return
59
+ ShadowSystemClock.advanceBy(Duration.ofMillis(100))
60
+ Thread.sleep(10)
61
+ shadowOf(looper).idle()
62
+ }
63
+ fail("the engine never became ready, in state ${player.exoPlayer.playbackState}")
64
+ }
65
+
66
+ private fun track() =
67
+ TestFixtures.track(id = "a", src = folder.silentWavUri(Duration.ofMinutes(2)))
68
+
69
+ @Test
70
+ fun `a seek right after a load lands`() {
71
+ player.load(track())
72
+ player.seekTo(60, TimeUnit.SECONDS)
73
+
74
+ idleUntilReady()
75
+
76
+ assertEquals(60_000L, player.exoPlayer.currentPosition)
77
+ assertEquals(60_000L, player.position)
78
+ }
79
+
80
+ @Test
81
+ fun `a seek right after a load that replaces the current track lands`() {
82
+ player.load(track())
83
+ idleUntilReady()
84
+
85
+ // Another file: media3 updates an item of the same URI in place, without a placeholder.
86
+ val other =
87
+ folder.newFile("other.wav").apply { writeBytes(silentWavBytes(Duration.ofMinutes(2))) }
88
+ player.load(TestFixtures.track(id = "b", src = Uri.fromFile(other).toString()))
89
+ player.seekTo(60, TimeUnit.SECONDS)
90
+ idleUntilReady()
91
+
92
+ assertEquals("b", player.currentTrack?.id)
93
+ assertEquals(60_000L, player.exoPlayer.currentPosition)
94
+ }
95
+
96
+ @Test
97
+ fun `a setQueue at a position then starts there`() {
98
+ player.setQueue(arrayOf(track()), 0, 60_000)
99
+
100
+ idleUntilReady()
101
+
102
+ assertEquals(60_000L, player.exoPlayer.currentPosition)
103
+ assertEquals(60_000L, player.position)
104
+ }
105
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-audio-browser",
3
- "version": "0.2.0-next.88",
3
+ "version": "0.2.0-next.89",
4
4
  "description": "react-native-audio-browser is a react native package built with Nitro",
5
5
  "keywords": [
6
6
  "react-native",