react-native-audio-browser 0.2.0-next.38 → 0.2.0-next.39

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.
@@ -319,10 +319,7 @@ class Service : MediaLibraryService(), MediaSessionService.Listener {
319
319
  override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaLibrarySession {
320
320
  Timber.d("onGetSession requested by: ${controllerInfo.packageName}")
321
321
 
322
- // The player is always set up by onCreate. Don't re-run setup() here for controllers that
323
- // connect before React Native does: setup() releases and recreates the ExoPlayer, which would
324
- // kill in-progress playback (e.g. playback resumption from a car/Bluetooth controller) when a
325
- // second controller connects while JS is still booting.
322
+ // No setup() here: default options would replace the ones setupPlayer applied.
326
323
  if (player.getCallbacks() == null) {
327
324
  Timber.w("External controller connecting before React Native setup - using default options")
328
325
  }
@@ -70,6 +70,13 @@ data class PlayerSetupOptions(
70
70
  val automaticBuffer: Boolean
71
71
  get() = rebufferBuffer == null
72
72
 
73
+ /**
74
+ * A copy that equals another's when both build the same engine: without the now-playing options,
75
+ * which apply without a rebuild. A formatter is a new object on every setup call.
76
+ */
77
+ fun engineOptions() =
78
+ copy(autoUpdateNowPlayingMetadata = true, nowPlayingMetadataFormatter = null)
79
+
73
80
  fun update(options: NativeSetupPlayerOptions) {
74
81
  // Android-specific options
75
82
  options.android?.let { android ->
@@ -0,0 +1,63 @@
1
+ package com.audiobrowser.player
2
+
3
+ import androidx.media3.common.C
4
+ import androidx.media3.common.MediaItem
5
+ import androidx.media3.common.PlaybackParameters
6
+ import androidx.media3.common.Player as MediaPlayer
7
+ import androidx.media3.exoplayer.ExoPlayer
8
+ import androidx.media3.exoplayer.source.ShuffleOrder
9
+
10
+ /**
11
+ * What an engine is playing, and how: what [Player.setup] carries from the engine it releases to
12
+ * the one it builds, so new setup options don't stop playback.
13
+ */
14
+ internal data class EnginePlayback(
15
+ val mediaItems: List<MediaItem>,
16
+ val index: Int,
17
+ /**
18
+ * [C.TIME_UNSET] for a live item, which resumes at the live edge, and for 0, which is also how a
19
+ * live item reads before it has loaded.
20
+ */
21
+ val positionMs: Long,
22
+ val prepared: Boolean,
23
+ val playWhenReady: Boolean,
24
+ val repeatMode: Int,
25
+ val shuffleModeEnabled: Boolean,
26
+ val shuffleOrder: ShuffleOrder,
27
+ val playbackParameters: PlaybackParameters,
28
+ val volume: Float,
29
+ val skipSilenceEnabled: Boolean,
30
+ ) {
31
+ fun restoreTo(exoPlayer: ExoPlayer) {
32
+ exoPlayer.repeatMode = repeatMode
33
+ exoPlayer.shuffleModeEnabled = shuffleModeEnabled
34
+ exoPlayer.playbackParameters = playbackParameters
35
+ exoPlayer.volume = volume
36
+ exoPlayer.skipSilenceEnabled = skipSilenceEnabled
37
+ if (mediaItems.isNotEmpty()) {
38
+ exoPlayer.setMediaItems(mediaItems, index, positionMs)
39
+ exoPlayer.setShuffleOrder(shuffleOrder)
40
+ if (prepared) exoPlayer.prepare()
41
+ }
42
+ exoPlayer.playWhenReady = playWhenReady
43
+ }
44
+
45
+ companion object {
46
+ fun of(exoPlayer: ExoPlayer) =
47
+ EnginePlayback(
48
+ mediaItems = (0 until exoPlayer.mediaItemCount).map { exoPlayer.getMediaItemAt(it) },
49
+ index = exoPlayer.currentMediaItemIndex,
50
+ positionMs =
51
+ exoPlayer.currentPosition.takeUnless { exoPlayer.isCurrentMediaItemLive || it == 0L }
52
+ ?: C.TIME_UNSET,
53
+ prepared = exoPlayer.playbackState != MediaPlayer.STATE_IDLE,
54
+ playWhenReady = exoPlayer.playWhenReady,
55
+ repeatMode = exoPlayer.repeatMode,
56
+ shuffleModeEnabled = exoPlayer.shuffleModeEnabled,
57
+ shuffleOrder = exoPlayer.shuffleOrder,
58
+ playbackParameters = exoPlayer.playbackParameters,
59
+ volume = exoPlayer.volume,
60
+ skipSilenceEnabled = exoPlayer.skipSilenceEnabled,
61
+ )
62
+ }
63
+ }
@@ -381,20 +381,28 @@ class Player(internal val context: Context) {
381
381
  exoPlayer.skipSilenceEnabled = value
382
382
  }
383
383
 
384
+ private var engineOptions: PlayerSetupOptions? = null
385
+
384
386
  /**
385
- * Sets up or recreates the ExoPlayer with the provided setup options. This method can be called
386
- * multiple times to change setup options.
387
+ * Applies [setupOptions]: builds the ExoPlayer on the first call, and rebuilds it only when the
388
+ * engine options differ from the ones it was built with. A rebuild carries over the queue, the
389
+ * position and the play intent, so playback that has started goes on with the new engine.
387
390
  */
388
391
  fun setup(setupOptions: PlayerSetupOptions) {
389
- Timber.Forest.d("Setting up player with new options")
390
-
391
392
  nowPlaying.enabled = setupOptions.autoUpdateNowPlayingMetadata
392
393
  // Wrap the Nitro Func into a plain suspend call so the updater (and its tests) never touch
393
394
  // bridge types; the double-await depth is unchanged (invoke -> bridge Promise -> JS promise).
394
395
  nowPlaying.formatter =
395
396
  setupOptions.nowPlayingMetadataFormatter?.let { f -> { params -> f.invoke(params).await() } }
396
397
 
397
- val isInitialSetup = !::exoPlayer.isInitialized
398
+ val newEngineOptions = setupOptions.engineOptions()
399
+ if (newEngineOptions == engineOptions) {
400
+ Timber.Forest.d("Setup options unchanged: keeping the player")
401
+ return
402
+ }
403
+ Timber.Forest.d("Setting up player with new options")
404
+ val isInitialSetup = engineOptions == null
405
+ val carried = if (isInitialSetup) null else EnginePlayback.of(exoPlayer)
398
406
 
399
407
  if (!isInitialSetup) {
400
408
  forwardingPlayer.removeListener(playerListener)
@@ -442,14 +450,8 @@ class Player(internal val context: Context) {
442
450
  loadControl = engine.loadControl
443
451
  mediaFactory = engine.mediaFactory
444
452
  exoPlayer = engine.exoPlayer
445
-
446
- // A rebuilt engine starts paused and fires no change event — sync the
447
- // eager cache and tell JS, or the stale true sticks (and the emit dedupe
448
- // would swallow the next real rise).
449
- if (!isInitialSetup) {
450
- playWhenReadyCache = false
451
- emitPlayWhenReadyChanged(false)
452
- }
453
+ // Before the listener and the session are attached, so neither hears the playback replayed.
454
+ carried?.restoreTo(exoPlayer)
453
455
 
454
456
  // Recreate forwarding player with new ExoPlayer
455
457
  forwardingPlayer =
@@ -478,11 +480,20 @@ class Player(internal val context: Context) {
478
480
  mediaSession.player = forwardingPlayer
479
481
  }
480
482
 
481
- setPlaybackState(PlaybackState.NONE)
483
+ // No listener heard the restore; its prepare() leaves the engine buffering.
484
+ setPlaybackState(
485
+ if (exoPlayer.playbackState == ExoPlayer.STATE_IDLE) PlaybackState.NONE
486
+ else PlaybackState.BUFFERING
487
+ )
488
+ // The new engine can refuse the carried intent (audio focus denied) without a change event:
489
+ // sync the eager cache and tell JS, or a stale true sticks.
490
+ playWhenReadyCache = exoPlayer.playWhenReady
491
+ emitPlayWhenReadyChanged(exoPlayer.playWhenReady)
482
492
  }
483
493
 
484
494
  // Set up automatic buffer management if enabled
485
495
  setupAutomaticBufferManager(setupOptions.automaticBuffer)
496
+ engineOptions = newEngineOptions
486
497
  }
487
498
 
488
499
  /** Sets up or tears down the automatic buffer manager based on the enabled flag. */
@@ -0,0 +1,92 @@
1
+ package com.audiobrowser.player
2
+
3
+ import androidx.media3.common.Player as MediaPlayer
4
+ import com.audiobrowser.Callbacks
5
+ import com.audiobrowser.TestFixtures
6
+ import com.audiobrowser.model.PlayerSetupOptions
7
+ import com.audiobrowser.util.TrackFactory
8
+ import com.margelo.nitro.audiobrowser.PlaybackState
9
+ import org.junit.After
10
+ import org.junit.Assert.assertEquals
11
+ import org.junit.Assert.assertNotSame
12
+ import org.junit.Assert.assertSame
13
+ import org.junit.Assert.assertTrue
14
+ import org.junit.Test
15
+ import org.junit.runner.RunWith
16
+ import org.mockito.Mockito.mock
17
+ import org.mockito.Mockito.mockingDetails
18
+ import org.robolectric.RobolectricTestRunner
19
+ import org.robolectric.RuntimeEnvironment
20
+
21
+ // Robolectric: Player builds a real ExoPlayer, which needs the framework and a main looper.
22
+ @RunWith(RobolectricTestRunner::class)
23
+ class PlayerSetupTest {
24
+
25
+ private val player = Player(RuntimeEnvironment.getApplication())
26
+
27
+ @After fun release() = player.exoPlayer.release()
28
+
29
+ /** Service.onCreate and AudioBrowser.onServiceConnected set up with the same options. */
30
+ @Test
31
+ fun setupWithTheSameOptionsKeepsTheEngine() {
32
+ player.setup(PlayerSetupOptions())
33
+ val engine = player.exoPlayer
34
+
35
+ player.setup(PlayerSetupOptions())
36
+ player.setup(PlayerSetupOptions(autoUpdateNowPlayingMetadata = false))
37
+
38
+ assertSame(engine, player.exoPlayer)
39
+ }
40
+
41
+ @Test
42
+ fun setupWithChangedOptionsRebuildsTheEngineAndKeepsPlaying() {
43
+ player.setup(PlayerSetupOptions())
44
+ val engine = player.exoPlayer
45
+ engine.setMediaItems(listOf(item("a"), item("b"), item("c")), 1, 5_000)
46
+ engine.repeatMode = MediaPlayer.REPEAT_MODE_ALL
47
+ engine.shuffleModeEnabled = true
48
+ engine.volume = 0.5f
49
+ engine.prepare()
50
+ engine.playWhenReady = true
51
+ player.setPlaybackState(PlaybackState.PLAYING)
52
+ val callbacks = mock(Callbacks::class.java)
53
+ player.setCallbacks(callbacks)
54
+
55
+ player.setup(PlayerSetupOptions(maxBuffer = 120_000.0))
56
+
57
+ val rebuilt = player.exoPlayer
58
+ assertNotSame(engine, rebuilt)
59
+ assertEquals(listOf("a", "b", "c"), player.tracks.map { it.title })
60
+ assertEquals(1, rebuilt.currentMediaItemIndex)
61
+ assertEquals(5_000, rebuilt.currentPosition)
62
+ assertEquals(MediaPlayer.REPEAT_MODE_ALL, rebuilt.repeatMode)
63
+ assertTrue(rebuilt.shuffleModeEnabled)
64
+ assertEquals(0.5f, rebuilt.volume)
65
+ assertTrue(rebuilt.playbackState != MediaPlayer.STATE_IDLE)
66
+ assertTrue(rebuilt.playWhenReady)
67
+ assertTrue(player.playWhenReadyCache)
68
+ assertEquals(PlaybackState.BUFFERING, player.playbackState)
69
+ assertEquals(
70
+ emptyList<String>(),
71
+ mockingDetails(callbacks)
72
+ .invocations
73
+ .map { it.method.name }
74
+ .filter { it == "onPlaybackActiveTrackChanged" },
75
+ )
76
+ }
77
+
78
+ @Test
79
+ fun setupWithChangedOptionsKeepsAnUnpreparedQueueStopped() {
80
+ player.setup(PlayerSetupOptions())
81
+ player.exoPlayer.setMediaItems(listOf(item("a")))
82
+
83
+ player.setup(PlayerSetupOptions(maxBuffer = 120_000.0))
84
+
85
+ assertEquals(listOf("a"), player.tracks.map { it.title })
86
+ assertEquals(MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
87
+ assertEquals(PlaybackState.NONE, player.playbackState)
88
+ }
89
+
90
+ private fun item(title: String) =
91
+ TrackFactory.toMedia3(TestFixtures.track(title = title, src = "https://s/$title.mp3"))
92
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-audio-browser",
3
- "version": "0.2.0-next.38",
3
+ "version": "0.2.0-next.39",
4
4
  "description": "react-native-audio-browser is a react native package built with Nitro",
5
5
  "keywords": [
6
6
  "react-native",