react-native-audio-browser 0.2.0-next.65 → 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/AudioBrowser.kt +40 -4
- 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/LaunchSettledTest.kt +81 -0
- 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
|
@@ -95,11 +95,14 @@ import kotlin.time.Duration.Companion.seconds
|
|
|
95
95
|
import kotlinx.coroutines.CancellationException
|
|
96
96
|
import kotlinx.coroutines.CompletableDeferred
|
|
97
97
|
import kotlinx.coroutines.CoroutineScope
|
|
98
|
+
import kotlinx.coroutines.CoroutineStart
|
|
99
|
+
import kotlinx.coroutines.DelicateCoroutinesApi
|
|
98
100
|
import kotlinx.coroutines.Job
|
|
99
101
|
import kotlinx.coroutines.MainScope
|
|
100
102
|
import kotlinx.coroutines.async
|
|
101
103
|
import kotlinx.coroutines.cancel
|
|
102
104
|
import kotlinx.coroutines.cancelChildren
|
|
105
|
+
import kotlinx.coroutines.ensureActive
|
|
103
106
|
import kotlinx.coroutines.launch
|
|
104
107
|
import kotlinx.coroutines.runBlocking
|
|
105
108
|
import kotlinx.coroutines.withTimeoutOrNull
|
|
@@ -230,7 +233,7 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
|
|
|
230
233
|
post { onBatteryWarningPendingChanged(BatteryWarningPendingChangedEvent(pending)) }
|
|
231
234
|
}
|
|
232
235
|
// Every setupPlayer waiting for the service awaits it. Replaced on disconnect, after
|
|
233
|
-
// cancelChildren has rejected its waiters.
|
|
236
|
+
// cancelChildren has rejected its waiters, and on a dead binding, after failing them.
|
|
234
237
|
private var connectionReady = CompletableDeferred<Unit>()
|
|
235
238
|
|
|
236
239
|
// Staged by setup options or by setters called before the player exists; last write wins, and
|
|
@@ -659,7 +662,7 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
|
|
|
659
662
|
}
|
|
660
663
|
|
|
661
664
|
override fun search(query: String): Promise<Array<Track>> {
|
|
662
|
-
return
|
|
665
|
+
return promise {
|
|
663
666
|
Timber.d("Searching for: $query")
|
|
664
667
|
// Rejects with the error a browse would raise but leaves the navigation error unset: a failed
|
|
665
668
|
// search must not replace the displayed page's state (mirrors iOS HybridAudioBrowser.search).
|
|
@@ -776,7 +779,7 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
|
|
|
776
779
|
// ============================================================================
|
|
777
780
|
|
|
778
781
|
override fun setupPlayer(options: NativeSetupPlayerOptions): Promise<Unit> {
|
|
779
|
-
return
|
|
782
|
+
return promise {
|
|
780
783
|
setupOptions.update(options)
|
|
781
784
|
// Staged to apply together with the engine below, or at service connect. Last write wins
|
|
782
785
|
// against the imperative setters.
|
|
@@ -788,7 +791,7 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
|
|
|
788
791
|
it.player.applyOptions(updateOptions)
|
|
789
792
|
it.player.setup(setupOptions)
|
|
790
793
|
applyPendingPlayerState(it.player)
|
|
791
|
-
return@
|
|
794
|
+
return@promise
|
|
792
795
|
}
|
|
793
796
|
|
|
794
797
|
if (!serviceBinding.bind(Intent(context, Service::class.java), this@AudioBrowser)) {
|
|
@@ -1165,10 +1168,27 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
|
|
|
1165
1168
|
Timber.d("AudioBrowser.onServiceDisconnected()")
|
|
1166
1169
|
}
|
|
1167
1170
|
|
|
1171
|
+
// Android never reconnects a dead binding: release it so the next setupPlayer binds afresh.
|
|
1172
|
+
override fun onBindingDied(name: ComponentName) = dropDeadBinding()
|
|
1173
|
+
|
|
1174
|
+
override fun onNullBinding(name: ComponentName) = dropDeadBinding()
|
|
1175
|
+
|
|
1176
|
+
private fun dropDeadBinding() {
|
|
1177
|
+
serviceBinding.unbind(this)
|
|
1178
|
+
connectionReady.completeExceptionally(IllegalStateException("AudioBrowserService binding died"))
|
|
1179
|
+
connectionReady = CompletableDeferred()
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1168
1182
|
// ============================================================================
|
|
1169
1183
|
// MARK: Internal Helpers
|
|
1170
1184
|
// ============================================================================
|
|
1171
1185
|
|
|
1186
|
+
private fun <T> promise(run: suspend () -> T): Promise<T> {
|
|
1187
|
+
val promise = Promise<T>()
|
|
1188
|
+
mainScope.launchSettled({ it.fold(promise::resolve, promise::reject) }, run)
|
|
1189
|
+
return promise
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1172
1192
|
private fun launchInScope(block: suspend () -> Unit) {
|
|
1173
1193
|
mainScope.launch { block() }
|
|
1174
1194
|
}
|
|
@@ -1554,6 +1574,22 @@ internal suspend fun formattedOrDefault(
|
|
|
1554
1574
|
return withTimeoutOrNull(FORMATTER_TIMEOUT) { hop.await() } ?: default
|
|
1555
1575
|
}
|
|
1556
1576
|
|
|
1577
|
+
/**
|
|
1578
|
+
* Runs [run] in this scope and hands its outcome to [settle] once. Unlike Nitro's `Promise.async`,
|
|
1579
|
+
* a scope cancelled before [run] starts still settles, with the cancellation, and [run] never runs.
|
|
1580
|
+
*/
|
|
1581
|
+
@OptIn(DelicateCoroutinesApi::class)
|
|
1582
|
+
internal fun <T> CoroutineScope.launchSettled(settle: (Result<T>) -> Unit, run: suspend () -> T) {
|
|
1583
|
+
launch(start = CoroutineStart.ATOMIC) {
|
|
1584
|
+
settle(
|
|
1585
|
+
runCatching {
|
|
1586
|
+
ensureActive()
|
|
1587
|
+
run()
|
|
1588
|
+
}
|
|
1589
|
+
)
|
|
1590
|
+
}
|
|
1591
|
+
}
|
|
1592
|
+
|
|
1557
1593
|
private fun httpStatusText(statusCode: Int): String {
|
|
1558
1594
|
return when (statusCode) {
|
|
1559
1595
|
400 -> "Bad Request"
|
|
@@ -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]) {
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
package com.audiobrowser
|
|
2
|
+
|
|
3
|
+
import kotlinx.coroutines.CancellationException
|
|
4
|
+
import kotlinx.coroutines.CompletableDeferred
|
|
5
|
+
import kotlinx.coroutines.CoroutineScope
|
|
6
|
+
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
|
7
|
+
import kotlinx.coroutines.cancel
|
|
8
|
+
import kotlinx.coroutines.test.StandardTestDispatcher
|
|
9
|
+
import kotlinx.coroutines.test.advanceUntilIdle
|
|
10
|
+
import kotlinx.coroutines.test.runTest
|
|
11
|
+
import org.junit.Assert.assertEquals
|
|
12
|
+
import org.junit.Assert.assertFalse
|
|
13
|
+
import org.junit.Assert.assertTrue
|
|
14
|
+
import org.junit.Test
|
|
15
|
+
|
|
16
|
+
@OptIn(ExperimentalCoroutinesApi::class)
|
|
17
|
+
class LaunchSettledTest {
|
|
18
|
+
|
|
19
|
+
@Test
|
|
20
|
+
fun `a call into a cancelled scope settles without running`() = runTest {
|
|
21
|
+
val scope = CoroutineScope(StandardTestDispatcher(testScheduler)).apply { cancel() }
|
|
22
|
+
val outcomes = mutableListOf<Result<Int>>()
|
|
23
|
+
var ran = false
|
|
24
|
+
|
|
25
|
+
scope.launchSettled({ outcomes += it }) {
|
|
26
|
+
ran = true
|
|
27
|
+
1
|
|
28
|
+
}
|
|
29
|
+
advanceUntilIdle()
|
|
30
|
+
|
|
31
|
+
assertFalse(ran)
|
|
32
|
+
assertEquals(1, outcomes.size)
|
|
33
|
+
assertTrue(outcomes.single().exceptionOrNull() is CancellationException)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
@Test
|
|
37
|
+
fun `a call whose scope is cancelled before it is dispatched settles without running`() =
|
|
38
|
+
runTest {
|
|
39
|
+
val scope = CoroutineScope(StandardTestDispatcher(testScheduler))
|
|
40
|
+
val outcomes = mutableListOf<Result<Int>>()
|
|
41
|
+
var ran = false
|
|
42
|
+
|
|
43
|
+
scope.launchSettled({ outcomes += it }) {
|
|
44
|
+
ran = true
|
|
45
|
+
1
|
|
46
|
+
}
|
|
47
|
+
scope.cancel()
|
|
48
|
+
advanceUntilIdle()
|
|
49
|
+
|
|
50
|
+
assertFalse(ran)
|
|
51
|
+
assertEquals(1, outcomes.size)
|
|
52
|
+
assertTrue(outcomes.single().exceptionOrNull() is CancellationException)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
@Test
|
|
56
|
+
fun `a call cancelled while it waits settles once`() = runTest {
|
|
57
|
+
val scope = CoroutineScope(StandardTestDispatcher(testScheduler))
|
|
58
|
+
val outcomes = mutableListOf<Result<Unit>>()
|
|
59
|
+
|
|
60
|
+
scope.launchSettled({ outcomes += it }) { CompletableDeferred<Unit>().await() }
|
|
61
|
+
advanceUntilIdle()
|
|
62
|
+
scope.cancel()
|
|
63
|
+
advanceUntilIdle()
|
|
64
|
+
|
|
65
|
+
assertEquals(1, outcomes.size)
|
|
66
|
+
assertTrue(outcomes.single().exceptionOrNull() is CancellationException)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
@Test
|
|
70
|
+
fun `a call settles with what it returns or throws`() = runTest {
|
|
71
|
+
val scope = CoroutineScope(StandardTestDispatcher(testScheduler))
|
|
72
|
+
val outcomes = mutableListOf<Result<Int>>()
|
|
73
|
+
val boom = IllegalStateException("boom")
|
|
74
|
+
|
|
75
|
+
scope.launchSettled({ outcomes += it }) { 1 }
|
|
76
|
+
scope.launchSettled({ outcomes += it }) { throw boom }
|
|
77
|
+
advanceUntilIdle()
|
|
78
|
+
|
|
79
|
+
assertEquals(listOf(Result.success(1), Result.failure<Int>(boom)), outcomes)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -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()
|