react-native-audio-browser 0.2.0-next.40 → 0.2.0-next.41
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.
|
@@ -60,6 +60,12 @@ class Service : MediaLibraryService(), MediaSessionService.Listener {
|
|
|
60
60
|
|
|
61
61
|
private var artworkProviderDeps: ArtworkProviderDeps? = null
|
|
62
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Whether AudioBrowser holds its [LocalBinder] binding. While it does, [stopSelf] cannot end the
|
|
65
|
+
* service, so a task removal keeps the player.
|
|
66
|
+
*/
|
|
67
|
+
private var moduleBound = false
|
|
68
|
+
|
|
63
69
|
// Headless service binding
|
|
64
70
|
private var headlessBound = false
|
|
65
71
|
private val headlessConnection: ServiceConnection =
|
|
@@ -255,13 +261,17 @@ class Service : MediaLibraryService(), MediaSessionService.Listener {
|
|
|
255
261
|
super.onBind(intent)
|
|
256
262
|
} else {
|
|
257
263
|
Timber.d("Service being bound by module - returning LocalBinder")
|
|
264
|
+
moduleBound = true
|
|
258
265
|
binder
|
|
259
266
|
}
|
|
260
267
|
}
|
|
261
268
|
|
|
262
|
-
override fun
|
|
263
|
-
|
|
269
|
+
override fun onRebind(intent: Intent?) {
|
|
270
|
+
if (intent?.action == null) moduleBound = true
|
|
271
|
+
super.onRebind(intent)
|
|
272
|
+
}
|
|
264
273
|
|
|
274
|
+
override fun onTaskRemoved(rootIntent: Intent?) {
|
|
265
275
|
val appKilledPlaybackBehavior = player.appKilledPlaybackBehavior
|
|
266
276
|
|
|
267
277
|
Timber.d("player = $player, appKilledPlaybackBehavior = $appKilledPlaybackBehavior")
|
|
@@ -288,18 +298,21 @@ class Service : MediaLibraryService(), MediaSessionService.Listener {
|
|
|
288
298
|
// Just pause and remove notification, but keep service alive for external controllers
|
|
289
299
|
player.pause()
|
|
290
300
|
stopForeground(STOP_FOREGROUND_REMOVE)
|
|
301
|
+
} else if (moduleBound) {
|
|
302
|
+
Timber.d("AudioBrowser still bound - stopping playback but keeping the player")
|
|
303
|
+
stopPlaybackAndRemoveNotification()
|
|
304
|
+
// Ends the service once AudioBrowser unbinds.
|
|
305
|
+
stopSelf()
|
|
291
306
|
} else {
|
|
292
307
|
Timber.d("No external controllers - proceeding with service shutdown")
|
|
293
308
|
try {
|
|
294
309
|
if (::mediaSession.isInitialized) {
|
|
295
310
|
mediaSession.release()
|
|
296
311
|
}
|
|
297
|
-
|
|
298
|
-
player.stop()
|
|
312
|
+
stopPlaybackAndRemoveNotification()
|
|
299
313
|
tearDownArtworkProvider()
|
|
300
314
|
player.destroy()
|
|
301
315
|
scope.cancel()
|
|
302
|
-
stopForeground(STOP_FOREGROUND_REMOVE)
|
|
303
316
|
stopSelf()
|
|
304
317
|
} catch (e: Exception) {
|
|
305
318
|
Timber.e(e, "Error during aggressive cleanup in onTaskRemoved")
|
|
@@ -316,6 +329,14 @@ class Service : MediaLibraryService(), MediaSessionService.Listener {
|
|
|
316
329
|
}
|
|
317
330
|
}
|
|
318
331
|
|
|
332
|
+
private fun stopPlaybackAndRemoveNotification() {
|
|
333
|
+
// Before stop(), while a mid-ramp fade can still restore the volume.
|
|
334
|
+
player.sleepTimer.clear()
|
|
335
|
+
player.clear()
|
|
336
|
+
player.stop()
|
|
337
|
+
stopForeground(STOP_FOREGROUND_REMOVE)
|
|
338
|
+
}
|
|
339
|
+
|
|
319
340
|
override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaLibrarySession {
|
|
320
341
|
Timber.d("onGetSession requested by: ${controllerInfo.packageName}")
|
|
321
342
|
|
|
@@ -329,6 +350,12 @@ class Service : MediaLibraryService(), MediaSessionService.Listener {
|
|
|
329
350
|
|
|
330
351
|
override fun onUnbind(intent: Intent?): Boolean {
|
|
331
352
|
Timber.d("onUnbind called - action: ${intent?.action}, package: ${intent?.`package`}")
|
|
353
|
+
if (intent?.action == null) {
|
|
354
|
+
moduleBound = false
|
|
355
|
+
// Android calls onBind once per distinct Intent while the service lives and caches the
|
|
356
|
+
// binder, so only onRebind tells of the next binding, and only when this returns true.
|
|
357
|
+
return true
|
|
358
|
+
}
|
|
332
359
|
return super.onUnbind(intent)
|
|
333
360
|
}
|
|
334
361
|
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
package com.audiobrowser
|
|
2
|
+
|
|
3
|
+
import android.content.ComponentName
|
|
4
|
+
import android.content.Intent
|
|
5
|
+
import android.os.Binder
|
|
6
|
+
import android.os.Looper
|
|
7
|
+
import com.audiobrowser.model.PlayerUpdateOptions
|
|
8
|
+
import com.margelo.nitro.audiobrowser.AppKilledPlaybackBehavior
|
|
9
|
+
import com.margelo.nitro.audiobrowser.PlaybackState
|
|
10
|
+
import org.junit.After
|
|
11
|
+
import org.junit.Assert.assertEquals
|
|
12
|
+
import org.junit.Assert.assertFalse
|
|
13
|
+
import org.junit.Assert.assertTrue
|
|
14
|
+
import org.junit.Test
|
|
15
|
+
import org.junit.runner.RunWith
|
|
16
|
+
import org.robolectric.Robolectric
|
|
17
|
+
import org.robolectric.RobolectricTestRunner
|
|
18
|
+
import org.robolectric.RuntimeEnvironment
|
|
19
|
+
import org.robolectric.Shadows.shadowOf
|
|
20
|
+
|
|
21
|
+
// Robolectric: the Service builds a real ExoPlayer and MediaLibrarySession, and ServiceController
|
|
22
|
+
// drives onBind/onUnbind/onRebind as the framework would.
|
|
23
|
+
@RunWith(RobolectricTestRunner::class)
|
|
24
|
+
class ServiceTaskRemovedTest {
|
|
25
|
+
|
|
26
|
+
private val controller = run {
|
|
27
|
+
// Robolectric connects an unregistered binding with a null ComponentName, which the
|
|
28
|
+
// HeadlessTaskService connection's non-null parameter rejects.
|
|
29
|
+
val application = RuntimeEnvironment.getApplication()
|
|
30
|
+
shadowOf(application)
|
|
31
|
+
.setComponentNameAndServiceForBindService(
|
|
32
|
+
ComponentName(application, HeadlessTaskService::class.java),
|
|
33
|
+
Binder(),
|
|
34
|
+
)
|
|
35
|
+
Robolectric.buildService(Service::class.java).create()
|
|
36
|
+
}
|
|
37
|
+
private val service = controller.get()
|
|
38
|
+
private val player = service.player
|
|
39
|
+
|
|
40
|
+
@After
|
|
41
|
+
fun destroy() {
|
|
42
|
+
controller.destroy()
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
private fun idle() = shadowOf(Looper.getMainLooper()).idle()
|
|
46
|
+
|
|
47
|
+
private fun playTrack(title: String) {
|
|
48
|
+
player.load(TestFixtures.track(title = title, src = "https://s/$title.mp3"))
|
|
49
|
+
player.play()
|
|
50
|
+
idle()
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private fun removeTask(behavior: AppKilledPlaybackBehavior? = null) {
|
|
54
|
+
behavior?.let { player.applyOptions(PlayerUpdateOptions(appKilledPlaybackBehavior = it)) }
|
|
55
|
+
service.onTaskRemoved(null)
|
|
56
|
+
idle()
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@Test
|
|
60
|
+
fun `a removed task stops playback and keeps the player while AudioBrowser is bound`() {
|
|
61
|
+
controller.bind()
|
|
62
|
+
playTrack("a")
|
|
63
|
+
|
|
64
|
+
removeTask()
|
|
65
|
+
|
|
66
|
+
assertFalse(player.exoPlayer.isReleased)
|
|
67
|
+
assertEquals(emptyList<String>(), player.tracks.map { it.title })
|
|
68
|
+
assertFalse(player.exoPlayer.playWhenReady)
|
|
69
|
+
assertEquals(PlaybackState.STOPPED, player.playbackState)
|
|
70
|
+
assertTrue(shadowOf(service).isForegroundStopped)
|
|
71
|
+
assertTrue(shadowOf(service).notificationShouldRemoved)
|
|
72
|
+
assertTrue(shadowOf(service).isStoppedBySelf)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
@Test
|
|
76
|
+
fun `a removed task clears the sleep timer of the player AudioBrowser kept`() {
|
|
77
|
+
controller.bind()
|
|
78
|
+
playTrack("a")
|
|
79
|
+
player.sleepTimer.setAfter(1_800.0)
|
|
80
|
+
|
|
81
|
+
removeTask()
|
|
82
|
+
|
|
83
|
+
assertTrue(player.sleepTimer.get().isFirst)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
@Test
|
|
87
|
+
fun `the player AudioBrowser kept plays again after the task is removed`() {
|
|
88
|
+
controller.bind()
|
|
89
|
+
playTrack("a")
|
|
90
|
+
removeTask()
|
|
91
|
+
|
|
92
|
+
playTrack("b")
|
|
93
|
+
|
|
94
|
+
assertFalse(player.exoPlayer.isReleased)
|
|
95
|
+
assertEquals(listOf("b"), player.tracks.map { it.title })
|
|
96
|
+
// Moves only on events from a live engine.
|
|
97
|
+
assertEquals(PlaybackState.BUFFERING, player.playbackState)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
@Test
|
|
101
|
+
fun `a removed task destroys the player when AudioBrowser never bound`() {
|
|
102
|
+
playTrack("a")
|
|
103
|
+
|
|
104
|
+
removeTask()
|
|
105
|
+
|
|
106
|
+
assertTrue(player.exoPlayer.isReleased)
|
|
107
|
+
assertTrue(shadowOf(service).isStoppedBySelf)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
@Test
|
|
111
|
+
fun `a removed task destroys the player once AudioBrowser has unbound`() {
|
|
112
|
+
controller.bind()
|
|
113
|
+
controller.unbind()
|
|
114
|
+
playTrack("a")
|
|
115
|
+
|
|
116
|
+
removeTask()
|
|
117
|
+
|
|
118
|
+
assertTrue(player.exoPlayer.isReleased)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
@Test
|
|
122
|
+
fun `AudioBrowser binding again after an unbind keeps the player through a removed task`() {
|
|
123
|
+
controller.bind()
|
|
124
|
+
// Called directly: ServiceController.unbind() drops the result, and rebind() calls onRebind
|
|
125
|
+
// whatever it was, where Android calls it only after true.
|
|
126
|
+
assertTrue(service.onUnbind(Intent(service, Service::class.java)))
|
|
127
|
+
controller.rebind()
|
|
128
|
+
playTrack("a")
|
|
129
|
+
|
|
130
|
+
removeTask()
|
|
131
|
+
|
|
132
|
+
assertFalse(player.exoPlayer.isReleased)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
@Test
|
|
136
|
+
fun `a removed task pauses and keeps the queue with PAUSE_PLAYBACK`() {
|
|
137
|
+
controller.bind()
|
|
138
|
+
playTrack("a")
|
|
139
|
+
|
|
140
|
+
removeTask(AppKilledPlaybackBehavior.PAUSE_PLAYBACK)
|
|
141
|
+
|
|
142
|
+
assertFalse(player.exoPlayer.isReleased)
|
|
143
|
+
assertEquals(listOf("a"), player.tracks.map { it.title })
|
|
144
|
+
assertFalse(player.exoPlayer.playWhenReady)
|
|
145
|
+
assertFalse(shadowOf(service).isStoppedBySelf)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
@Test
|
|
149
|
+
fun `a removed task leaves playback running with CONTINUE_PLAYBACK`() {
|
|
150
|
+
controller.bind()
|
|
151
|
+
playTrack("a")
|
|
152
|
+
|
|
153
|
+
removeTask(AppKilledPlaybackBehavior.CONTINUE_PLAYBACK)
|
|
154
|
+
|
|
155
|
+
assertEquals(listOf("a"), player.tracks.map { it.title })
|
|
156
|
+
assertTrue(player.exoPlayer.playWhenReady)
|
|
157
|
+
assertFalse(shadowOf(service).isStoppedBySelf)
|
|
158
|
+
}
|
|
159
|
+
}
|
|
@@ -4,6 +4,7 @@ import android.content.Context
|
|
|
4
4
|
import android.media.AudioManager
|
|
5
5
|
import android.net.Uri
|
|
6
6
|
import android.os.Looper
|
|
7
|
+
import androidx.media3.common.C
|
|
7
8
|
import androidx.media3.common.Player as MediaPlayer
|
|
8
9
|
import com.audiobrowser.Callbacks
|
|
9
10
|
import com.audiobrowser.TestFixtures
|
|
@@ -35,9 +36,10 @@ import org.robolectric.Shadows.shadowOf
|
|
|
35
36
|
import org.robolectric.annotation.Config
|
|
36
37
|
|
|
37
38
|
// Robolectric: Player builds a real ExoPlayer, whose audio focus handling the framework's
|
|
38
|
-
// AudioManager drives.
|
|
39
|
-
//
|
|
40
|
-
//
|
|
39
|
+
// AudioManager drives. Robolectric's clock stands still, so a delayed message on ExoPlayer's
|
|
40
|
+
// playback thread never runs: the audio track is disabled because an audio renderer can wait in
|
|
41
|
+
// BUFFERING on one. SDK 28: from 33, media3 asks AudioManager.getAudioDevicesForAttributes,
|
|
42
|
+
// which Robolectric 4.11 answers with null.
|
|
41
43
|
@RunWith(RobolectricTestRunner::class)
|
|
42
44
|
@Config(sdk = [28])
|
|
43
45
|
class PlayerSuppressionTest {
|
|
@@ -55,6 +57,11 @@ class PlayerSuppressionTest {
|
|
|
55
57
|
player.setup(PlayerSetupOptions())
|
|
56
58
|
player.setCallbacks(callbacks)
|
|
57
59
|
val src = Uri.fromFile(silentWav()).toString()
|
|
60
|
+
player.exoPlayer.trackSelectionParameters =
|
|
61
|
+
player.exoPlayer.trackSelectionParameters
|
|
62
|
+
.buildUpon()
|
|
63
|
+
.setTrackTypeDisabled(C.TRACK_TYPE_AUDIO, true)
|
|
64
|
+
.build()
|
|
58
65
|
player.exoPlayer.setMediaItems(
|
|
59
66
|
listOf(TrackFactory.toMedia3(TestFixtures.track(title = "a", src = src)))
|
|
60
67
|
)
|