react-native-audio-browser 0.2.0-next.66 → 0.2.0-next.67
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.
- package/android/src/main/java/com/audiobrowser/Service.kt +16 -12
- package/android/src/main/java/com/audiobrowser/player/Player.kt +6 -10
- package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +2 -1
- package/android/src/test/java/com/audiobrowser/LegacyRatingTest.kt +1 -1
- package/android/src/test/java/com/audiobrowser/ServiceTaskRemovedTest.kt +85 -0
- package/android/src/test/java/com/audiobrowser/player/PlayerRetryTest.kt +49 -7
- package/ios/Player/Player.swift +1 -1
- package/ios/Tests/PlayerTests.swift +15 -0
- package/package.json +1 -1
|
@@ -37,6 +37,7 @@ import kotlinx.coroutines.Dispatchers
|
|
|
37
37
|
import kotlinx.coroutines.MainScope
|
|
38
38
|
import kotlinx.coroutines.SupervisorJob
|
|
39
39
|
import kotlinx.coroutines.cancel
|
|
40
|
+
import kotlinx.coroutines.isActive
|
|
40
41
|
import timber.log.Timber
|
|
41
42
|
|
|
42
43
|
/**
|
|
@@ -244,19 +245,12 @@ class Service : MediaLibraryService(), MediaSessionService.Listener {
|
|
|
244
245
|
super.onRebind(intent)
|
|
245
246
|
}
|
|
246
247
|
|
|
248
|
+
@OptIn(UnstableApi::class)
|
|
247
249
|
override fun onTaskRemoved(rootIntent: Intent?) {
|
|
248
250
|
val appKilledPlaybackBehavior = player.appKilledPlaybackBehavior
|
|
249
251
|
|
|
250
252
|
Timber.d("player = $player, appKilledPlaybackBehavior = $appKilledPlaybackBehavior")
|
|
251
253
|
|
|
252
|
-
// The system UI's controller backs the notification, so it doesn't count as external.
|
|
253
|
-
val hasExternalControllers =
|
|
254
|
-
mediaSession.connectedControllers.any { controller ->
|
|
255
|
-
controller.packageName != packageName && controller.packageName != "com.android.systemui"
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
Timber.d("hasExternalControllers = $hasExternalControllers")
|
|
259
|
-
|
|
260
254
|
when (appKilledPlaybackBehavior) {
|
|
261
255
|
AppKilledPlaybackBehavior.PAUSE_PLAYBACK -> {
|
|
262
256
|
Timber.d("Pausing playback - appKilledPlaybackBehavior = $appKilledPlaybackBehavior")
|
|
@@ -264,8 +258,14 @@ class Service : MediaLibraryService(), MediaSessionService.Listener {
|
|
|
264
258
|
}
|
|
265
259
|
|
|
266
260
|
AppKilledPlaybackBehavior.STOP_PLAYBACK_AND_REMOVE_NOTIFICATION -> {
|
|
267
|
-
|
|
268
|
-
|
|
261
|
+
// Only a car keeps the session through a swipe: the Bluetooth stack connects as a
|
|
262
|
+
// controller whenever Bluetooth is on, with or without a device.
|
|
263
|
+
val carConnected =
|
|
264
|
+
mediaSession.connectedControllers.any {
|
|
265
|
+
mediaSession.isAutomotiveController(it) || mediaSession.isAutoCompanionController(it)
|
|
266
|
+
}
|
|
267
|
+
if (carConnected) {
|
|
268
|
+
Timber.d("A car is connected - pausing instead of stopping")
|
|
269
269
|
player.pause()
|
|
270
270
|
stopForeground(STOP_FOREGROUND_REMOVE)
|
|
271
271
|
} else if (moduleBound) {
|
|
@@ -274,7 +274,7 @@ class Service : MediaLibraryService(), MediaSessionService.Listener {
|
|
|
274
274
|
// Ends the service once AudioBrowser unbinds.
|
|
275
275
|
stopSelf()
|
|
276
276
|
} else {
|
|
277
|
-
Timber.d("No
|
|
277
|
+
Timber.d("No car connected - shutting the service down")
|
|
278
278
|
try {
|
|
279
279
|
if (::mediaSession.isInitialized) {
|
|
280
280
|
mediaSession.release()
|
|
@@ -305,9 +305,13 @@ class Service : MediaLibraryService(), MediaSessionService.Listener {
|
|
|
305
305
|
stopForeground(STOP_FOREGROUND_REMOVE)
|
|
306
306
|
}
|
|
307
307
|
|
|
308
|
-
override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaLibrarySession {
|
|
308
|
+
override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaLibrarySession? {
|
|
309
309
|
Timber.d("onGetSession requested by: ${controllerInfo.packageName}")
|
|
310
310
|
|
|
311
|
+
// Cancelled with the session's release. A client still bound keeps the service alive after
|
|
312
|
+
// a swipe's shutdown, and Media3 throws on adding a released session.
|
|
313
|
+
if (!scope.isActive) return null
|
|
314
|
+
|
|
311
315
|
// No setup() here: default options would replace the ones setupPlayer applied.
|
|
312
316
|
if (player.getCallbacks() == null) {
|
|
313
317
|
Timber.w("External controller connecting before React Native setup - using default options")
|
|
@@ -578,20 +578,16 @@ internal constructor(
|
|
|
578
578
|
sessionPlayer.prepare()
|
|
579
579
|
}
|
|
580
580
|
|
|
581
|
-
/** @throws IllegalArgumentException if [index] is out of bounds. */
|
|
582
|
-
fun remove(index: Int) {
|
|
583
|
-
validateIndex(index)
|
|
584
|
-
sessionPlayer.removeMediaItem(index)
|
|
585
|
-
}
|
|
586
|
-
|
|
587
581
|
/** @throws IllegalArgumentException if an index is out of bounds or repeated. */
|
|
588
582
|
fun remove(indexes: List<Int>) {
|
|
589
583
|
if (indexes.toSet().size != indexes.size) {
|
|
590
584
|
throw IllegalArgumentException("Duplicate indexes provided")
|
|
591
585
|
}
|
|
592
586
|
indexes.forEach { validateIndex(it) }
|
|
593
|
-
val
|
|
594
|
-
|
|
587
|
+
val removesCurrent = sessionPlayer.currentMediaItemIndex in indexes
|
|
588
|
+
indexes.sortedDescending().forEach { sessionPlayer.removeMediaItem(it) }
|
|
589
|
+
// Loads the new current track on an engine an error left idle; a no-op otherwise.
|
|
590
|
+
if (removesCurrent && playWhenReady) prepare()
|
|
595
591
|
}
|
|
596
592
|
|
|
597
593
|
fun next() {
|
|
@@ -933,10 +929,10 @@ internal constructor(
|
|
|
933
929
|
|
|
934
930
|
fun togglePlayback() {
|
|
935
931
|
when {
|
|
936
|
-
shouldPlayWhenReady -> pause()
|
|
937
932
|
// Suppressed with the intent raised: ExoPlayer resumes when focus returns. play() would
|
|
938
933
|
// request focus again, and during a call that refusal drops the intent.
|
|
939
|
-
playWhenReady -> Unit
|
|
934
|
+
playWhenReady && !shouldPlayWhenReady -> Unit
|
|
935
|
+
PlayingStateFactory.derive(shouldPlayWhenReady, playbackState).playing -> pause()
|
|
940
936
|
else -> play()
|
|
941
937
|
}
|
|
942
938
|
}
|
|
@@ -151,7 +151,8 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
151
151
|
}
|
|
152
152
|
|
|
153
153
|
override fun onEvents(media3Player: MediaPlayer, events: MediaPlayer.Events) {
|
|
154
|
-
// In event order
|
|
154
|
+
// In event-constant order, not the order the events happened; the transitions rely on it.
|
|
155
|
+
// Each setPlaybackState emits.
|
|
155
156
|
for (i in 0 until events.size()) {
|
|
156
157
|
val event =
|
|
157
158
|
when (events[i]) {
|
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
package com.audiobrowser
|
|
2
2
|
|
|
3
3
|
import android.content.ComponentName
|
|
4
|
+
import android.content.ContextWrapper
|
|
4
5
|
import android.content.Intent
|
|
5
6
|
import android.os.Binder
|
|
7
|
+
import android.os.Bundle
|
|
6
8
|
import android.os.Looper
|
|
9
|
+
import android.os.Process
|
|
10
|
+
import androidx.annotation.OptIn
|
|
7
11
|
import androidx.media3.common.C
|
|
12
|
+
import androidx.media3.common.util.UnstableApi
|
|
13
|
+
import androidx.media3.session.MediaController
|
|
14
|
+
import androidx.media3.session.MediaSession
|
|
8
15
|
import com.audiobrowser.model.PlayerUpdateOptions
|
|
9
16
|
import com.audiobrowser.player.PlaybackStateStore
|
|
10
17
|
import com.margelo.nitro.audiobrowser.AppKilledPlaybackBehavior
|
|
@@ -14,6 +21,7 @@ import java.util.concurrent.TimeUnit
|
|
|
14
21
|
import org.junit.After
|
|
15
22
|
import org.junit.Assert.assertEquals
|
|
16
23
|
import org.junit.Assert.assertFalse
|
|
24
|
+
import org.junit.Assert.assertNull
|
|
17
25
|
import org.junit.Assert.assertTrue
|
|
18
26
|
import org.junit.Test
|
|
19
27
|
import org.junit.runner.RunWith
|
|
@@ -52,6 +60,35 @@ class ServiceTaskRemovedTest {
|
|
|
52
60
|
idle()
|
|
53
61
|
}
|
|
54
62
|
|
|
63
|
+
@OptIn(UnstableApi::class)
|
|
64
|
+
private fun connectController(packageName: String) {
|
|
65
|
+
val application = RuntimeEnvironment.getApplication()
|
|
66
|
+
shadowOf(application.packageManager)
|
|
67
|
+
.setPackagesForUid(Process.myUid(), application.packageName, packageName)
|
|
68
|
+
val context =
|
|
69
|
+
object : ContextWrapper(application) {
|
|
70
|
+
override fun getPackageName() = packageName
|
|
71
|
+
}
|
|
72
|
+
// Only to reach the private session: onGetSession ignores its argument.
|
|
73
|
+
val session =
|
|
74
|
+
service.onGetSession(
|
|
75
|
+
MediaSession.ControllerInfo.createTestOnlyControllerInfo(
|
|
76
|
+
packageName,
|
|
77
|
+
0,
|
|
78
|
+
Process.myUid(),
|
|
79
|
+
0,
|
|
80
|
+
0,
|
|
81
|
+
false,
|
|
82
|
+
Bundle.EMPTY,
|
|
83
|
+
false,
|
|
84
|
+
)
|
|
85
|
+
)
|
|
86
|
+
val future = MediaController.Builder(context, session!!.token).buildAsync()
|
|
87
|
+
idle()
|
|
88
|
+
// Throws if the session refused the controller.
|
|
89
|
+
future.get()
|
|
90
|
+
}
|
|
91
|
+
|
|
55
92
|
private fun removeTask(behavior: AppKilledPlaybackBehavior? = null) {
|
|
56
93
|
behavior?.let { player.applyOptions(PlayerUpdateOptions(appKilledPlaybackBehavior = it)) }
|
|
57
94
|
service.onTaskRemoved(null)
|
|
@@ -198,4 +235,52 @@ class ServiceTaskRemovedTest {
|
|
|
198
235
|
assertTrue(player.exoPlayer.playWhenReady)
|
|
199
236
|
assertFalse(shadowOf(service).isStoppedBySelf)
|
|
200
237
|
}
|
|
238
|
+
|
|
239
|
+
@Test
|
|
240
|
+
fun `a removed task stops playback while the Bluetooth stack is connected as a controller`() {
|
|
241
|
+
controller.bind()
|
|
242
|
+
playTrack("a")
|
|
243
|
+
connectController("com.google.android.bluetooth")
|
|
244
|
+
|
|
245
|
+
removeTask()
|
|
246
|
+
|
|
247
|
+
assertEquals(emptyList<String>(), player.tracks.map { it.title })
|
|
248
|
+
assertEquals(PlaybackState.STOPPED, player.playbackState)
|
|
249
|
+
assertTrue(shadowOf(service).isStoppedBySelf)
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
@Test
|
|
253
|
+
fun `a removed task only pauses while Android Auto is connected`() {
|
|
254
|
+
playTrack("a")
|
|
255
|
+
connectController("com.google.android.projection.gearhead")
|
|
256
|
+
|
|
257
|
+
removeTask()
|
|
258
|
+
|
|
259
|
+
assertFalse(player.exoPlayer.isReleased)
|
|
260
|
+
assertEquals(listOf("a"), player.tracks.map { it.title })
|
|
261
|
+
assertFalse(player.exoPlayer.playWhenReady)
|
|
262
|
+
assertTrue(shadowOf(service).isForegroundStopped)
|
|
263
|
+
assertTrue(shadowOf(service).notificationShouldRemoved)
|
|
264
|
+
assertFalse(shadowOf(service).isStoppedBySelf)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
@Test
|
|
268
|
+
fun `a removed task only pauses while Android Automotive is connected`() {
|
|
269
|
+
playTrack("a")
|
|
270
|
+
connectController("com.android.car.media")
|
|
271
|
+
|
|
272
|
+
removeTask()
|
|
273
|
+
|
|
274
|
+
assertFalse(player.exoPlayer.isReleased)
|
|
275
|
+
assertEquals(listOf("a"), player.tracks.map { it.title })
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
@Test
|
|
279
|
+
fun `a media browser binding after the task is removed gets no session`() {
|
|
280
|
+
playTrack("a")
|
|
281
|
+
connectController("com.google.android.bluetooth")
|
|
282
|
+
removeTask()
|
|
283
|
+
|
|
284
|
+
assertNull(service.onBind(Intent("android.media.browse.MediaBrowserService")))
|
|
285
|
+
}
|
|
201
286
|
}
|
|
@@ -103,6 +103,16 @@ class PlayerRetryTest {
|
|
|
103
103
|
verify(callbacks, never()).onPlaybackError(null)
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
+
@Test
|
|
107
|
+
fun `a toggle after a terminal error reloads`() {
|
|
108
|
+
failTerminally()
|
|
109
|
+
|
|
110
|
+
player.togglePlayback()
|
|
111
|
+
|
|
112
|
+
assertTrue(player.exoPlayer.playWhenReady)
|
|
113
|
+
assertEquals(MediaPlayer.STATE_BUFFERING, player.exoPlayer.playbackState)
|
|
114
|
+
}
|
|
115
|
+
|
|
106
116
|
@Test
|
|
107
117
|
fun `a pause during a retry stays paused and the next play reloads`() {
|
|
108
118
|
pauseDuringRetry { player.pause() }
|
|
@@ -122,8 +132,8 @@ class PlayerRetryTest {
|
|
|
122
132
|
}
|
|
123
133
|
|
|
124
134
|
/** Pauses with a retry pending and runs past every backoff it would have taken. */
|
|
125
|
-
private fun pauseDuringRetry(pause: () -> Unit) {
|
|
126
|
-
play("https://127.0.0.1:9/stream", RetryPolicy.Infinite())
|
|
135
|
+
private fun pauseDuringRetry(vararg next: String, pause: () -> Unit) {
|
|
136
|
+
play("https://127.0.0.1:9/stream", *next, retryPolicy = RetryPolicy.Infinite())
|
|
127
137
|
awaitRetry("a pending retry")
|
|
128
138
|
|
|
129
139
|
pause()
|
|
@@ -149,6 +159,36 @@ class PlayerRetryTest {
|
|
|
149
159
|
player.playbackError?.retrying == true
|
|
150
160
|
}
|
|
151
161
|
|
|
162
|
+
@Test
|
|
163
|
+
fun `removing a failed current track plays the next`() {
|
|
164
|
+
failTerminally(Uri.fromFile(silentWav()).toString())
|
|
165
|
+
|
|
166
|
+
player.remove(listOf(0))
|
|
167
|
+
|
|
168
|
+
idleUntil("the next track playing") { player.playbackState == PlaybackState.PLAYING }
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
@Test
|
|
172
|
+
fun `removing another track keeps a terminal error`() {
|
|
173
|
+
failTerminally(Uri.fromFile(silentWav()).toString())
|
|
174
|
+
|
|
175
|
+
player.remove(listOf(1))
|
|
176
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
177
|
+
|
|
178
|
+
assertEquals(MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
|
|
179
|
+
assertEquals(PlaybackState.ERROR, player.playbackState)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
@Test
|
|
183
|
+
fun `removing the current track after a cancelled retry stays idle`() {
|
|
184
|
+
pauseDuringRetry(Uri.fromFile(silentWav()).toString()) { player.pause() }
|
|
185
|
+
|
|
186
|
+
player.remove(listOf(0))
|
|
187
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
188
|
+
|
|
189
|
+
assertEquals(MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
|
|
190
|
+
}
|
|
191
|
+
|
|
152
192
|
@Test
|
|
153
193
|
fun `emptying the queue clears a terminal error`() {
|
|
154
194
|
failTerminally()
|
|
@@ -167,7 +207,7 @@ class PlayerRetryTest {
|
|
|
167
207
|
connectivity.networkCallbacks.forEach { it.onAvailable(wifi) }
|
|
168
208
|
// A closed port: the load fails at once, the retry is pending, and the next failure is
|
|
169
209
|
// terminal.
|
|
170
|
-
play("https://127.0.0.1:9/stream", RetryPolicy.Limited(maxRetries = 1))
|
|
210
|
+
play("https://127.0.0.1:9/stream", retryPolicy = RetryPolicy.Limited(maxRetries = 1))
|
|
171
211
|
player.observeNetworkConnectivity(scope)
|
|
172
212
|
idleUntil("the retry gave up") {
|
|
173
213
|
ShadowSystemClock.advanceBy(Duration.ofSeconds(1))
|
|
@@ -193,15 +233,15 @@ class PlayerRetryTest {
|
|
|
193
233
|
}
|
|
194
234
|
|
|
195
235
|
/** A closed port: the load fails at once, and with no retries the failure is terminal. */
|
|
196
|
-
private fun failTerminally() {
|
|
197
|
-
play("https://127.0.0.1:9/stream", RetryPolicy.Limited(maxRetries = 0))
|
|
236
|
+
private fun failTerminally(vararg next: String) {
|
|
237
|
+
play("https://127.0.0.1:9/stream", *next, retryPolicy = RetryPolicy.Limited(maxRetries = 0))
|
|
198
238
|
idleUntil("a terminal error") {
|
|
199
239
|
ShadowSystemClock.advanceBy(Duration.ofSeconds(1))
|
|
200
240
|
player.playbackState == PlaybackState.ERROR
|
|
201
241
|
}
|
|
202
242
|
}
|
|
203
243
|
|
|
204
|
-
private fun play(
|
|
244
|
+
private fun play(vararg srcs: String, retryPolicy: RetryPolicy = RetryPolicy.Default) {
|
|
205
245
|
player.setup(PlayerSetupOptions(retryPolicy = retryPolicy))
|
|
206
246
|
player.setCallbacks(callbacks)
|
|
207
247
|
player.exoPlayer.trackSelectionParameters =
|
|
@@ -210,7 +250,9 @@ class PlayerRetryTest {
|
|
|
210
250
|
.setTrackTypeDisabled(C.TRACK_TYPE_AUDIO, true)
|
|
211
251
|
.build()
|
|
212
252
|
player.exoPlayer.setMediaItems(
|
|
213
|
-
|
|
253
|
+
srcs.mapIndexed { i, src ->
|
|
254
|
+
TrackFactory.toMedia3(TestFixtures.track(title = "$i", src = src))
|
|
255
|
+
}
|
|
214
256
|
)
|
|
215
257
|
player.exoPlayer.prepare()
|
|
216
258
|
player.play()
|
package/ios/Player/Player.swift
CHANGED
|
@@ -538,6 +538,21 @@ struct PlayWhenReadyTests {
|
|
|
538
538
|
#expect(c.deferredSeek.target == 42)
|
|
539
539
|
}
|
|
540
540
|
|
|
541
|
+
@Test @MainActor
|
|
542
|
+
func toggle_fromErrorWithIntentRaised_reloads() throws {
|
|
543
|
+
let (c, eh, _, _) = makeCoordinator()
|
|
544
|
+
try loadTrack(c)
|
|
545
|
+
c.setPlayWhenReady(true)
|
|
546
|
+
c.transition(.errorOccurred(.playbackFailed))
|
|
547
|
+
let resolvesBefore = eh.resolveAndLoadCalls.count
|
|
548
|
+
|
|
549
|
+
c.togglePlayback()
|
|
550
|
+
|
|
551
|
+
#expect(c.playWhenReady)
|
|
552
|
+
#expect(eh.resolveAndLoadCalls.count == resolvesBefore + 1)
|
|
553
|
+
#expect(c.state == .loading)
|
|
554
|
+
}
|
|
555
|
+
|
|
541
556
|
@Test @MainActor
|
|
542
557
|
func true_fromStopped_triggersReload() throws {
|
|
543
558
|
let (c, eh, _, _) = makeCoordinator()
|