react-native-audio-browser 0.2.0-next.40 → 0.2.0-next.42
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 +32 -5
- package/android/src/main/java/com/audiobrowser/player/MediaSessionCommandManager.kt +37 -12
- package/android/src/main/java/com/audiobrowser/player/Player.kt +5 -0
- package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +4 -0
- package/android/src/test/java/com/audiobrowser/ServiceTaskRemovedTest.kt +159 -0
- package/android/src/test/java/com/audiobrowser/player/LiveSeekCommandTest.kt +138 -0
- package/android/src/test/java/com/audiobrowser/player/PlayerSuppressionTest.kt +10 -3
- package/package.json +1 -1
|
@@ -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
|
|
|
@@ -90,7 +90,7 @@ class MediaSessionCommandManager {
|
|
|
90
90
|
else -> CommandButton.ICON_SKIP_BACK
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
-
/** Player commands available to every controller, derived from capabilities
|
|
93
|
+
/** Player commands available to every controller, derived from capabilities and liveness */
|
|
94
94
|
var playerCommands: MediaPlayer.Commands = MediaSession.ConnectionResult.DEFAULT_PLAYER_COMMANDS
|
|
95
95
|
private set
|
|
96
96
|
|
|
@@ -120,6 +120,7 @@ class MediaSessionCommandManager {
|
|
|
120
120
|
)
|
|
121
121
|
private var currentRemoteButtonLayout: RemoteButtonLayout? = null
|
|
122
122
|
private var currentFavorited: Boolean? = null
|
|
123
|
+
private var currentLive: Boolean = false
|
|
123
124
|
private var currentForwardJumpInterval: Double = DEFAULT_JUMP_INTERVAL
|
|
124
125
|
private var currentBackwardJumpInterval: Double = DEFAULT_JUMP_INTERVAL
|
|
125
126
|
|
|
@@ -142,7 +143,7 @@ class MediaSessionCommandManager {
|
|
|
142
143
|
// Initialize with defaults matching PlayerUpdateOptions (all capabilities enabled)
|
|
143
144
|
val defaultCapabilities = currentCapabilities
|
|
144
145
|
|
|
145
|
-
playerCommands = buildPlayerCommands(defaultCapabilities)
|
|
146
|
+
playerCommands = buildPlayerCommands(defaultCapabilities, live = false)
|
|
146
147
|
sessionCommands = buildSessionCommands(defaultCapabilities, searchAvailable = false)
|
|
147
148
|
buttonPreferences =
|
|
148
149
|
buildButtonPreferences(
|
|
@@ -183,22 +184,39 @@ class MediaSessionCommandManager {
|
|
|
183
184
|
currentForwardJumpInterval = forwardJumpInterval
|
|
184
185
|
currentBackwardJumpInterval = backwardJumpInterval
|
|
185
186
|
|
|
186
|
-
// Commands gate on capabilities
|
|
187
|
-
//
|
|
188
|
-
playerCommands = buildPlayerCommands(capabilities)
|
|
187
|
+
// Commands gate on capabilities and liveness, never on placement, so a control left out of the
|
|
188
|
+
// layout still works from a Bluetooth remote or headset.
|
|
189
|
+
playerCommands = buildPlayerCommands(capabilities, currentLive)
|
|
189
190
|
sessionCommands = buildSessionCommands(capabilities, searchAvailable)
|
|
190
191
|
buttonPreferences =
|
|
191
192
|
buildButtonPreferences(deriveButtonSlots(capabilities, remoteButtonLayout), currentFavorited)
|
|
192
193
|
|
|
193
|
-
//
|
|
194
|
-
//
|
|
195
|
-
|
|
196
|
-
|
|
194
|
+
// Granted before the layout broadcast so a newly-added button arrives with its command already
|
|
195
|
+
// available; Media3 disables buttons whose backing command is missing.
|
|
196
|
+
publishAvailableCommands(mediaSession)
|
|
197
|
+
publishButtonPreferences(mediaSession)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Withholds seeking within the current item while it is live, whatever the `seekTo` capability
|
|
202
|
+
* says: a controller offered that command draws a seek bar, which a live stream has nothing to
|
|
203
|
+
* fill. Call this whenever the current track may have changed.
|
|
204
|
+
*/
|
|
205
|
+
fun updateLiveState(mediaSession: MediaSession, live: Boolean) {
|
|
206
|
+
if (currentLive == live) return
|
|
207
|
+
currentLive = live
|
|
208
|
+
playerCommands = buildPlayerCommands(currentCapabilities, live)
|
|
209
|
+
publishAvailableCommands(mediaSession)
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Pushes the commands to already-connected controllers — [buildConnectionResult] only serves
|
|
214
|
+
* controllers that connect later, and an Android Auto session lives until the car shuts down.
|
|
215
|
+
*/
|
|
216
|
+
private fun publishAvailableCommands(mediaSession: MediaSession) {
|
|
197
217
|
mediaSession.connectedControllers.forEach {
|
|
198
218
|
mediaSession.setAvailableCommands(it, sessionCommands, playerCommands)
|
|
199
219
|
}
|
|
200
|
-
|
|
201
|
-
publishButtonPreferences(mediaSession)
|
|
202
220
|
}
|
|
203
221
|
|
|
204
222
|
/**
|
|
@@ -301,7 +319,10 @@ class MediaSessionCommandManager {
|
|
|
301
319
|
}
|
|
302
320
|
}
|
|
303
321
|
|
|
304
|
-
private fun buildPlayerCommands(
|
|
322
|
+
private fun buildPlayerCommands(
|
|
323
|
+
capabilities: PlayerCapabilities,
|
|
324
|
+
live: Boolean,
|
|
325
|
+
): MediaPlayer.Commands {
|
|
305
326
|
val playerCommandsBuilder = MediaSession.ConnectionResult.DEFAULT_PLAYER_COMMANDS.buildUpon()
|
|
306
327
|
|
|
307
328
|
// Always filter out direct media item commands to avoid dual-command confusion.
|
|
@@ -316,6 +337,10 @@ class MediaSessionCommandManager {
|
|
|
316
337
|
}
|
|
317
338
|
}
|
|
318
339
|
|
|
340
|
+
if (live) {
|
|
341
|
+
playerCommandsBuilder.remove(MediaPlayer.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM)
|
|
342
|
+
}
|
|
343
|
+
|
|
319
344
|
return playerCommandsBuilder.build()
|
|
320
345
|
}
|
|
321
346
|
|
|
@@ -933,6 +933,11 @@ class Player(internal val context: Context) {
|
|
|
933
933
|
mediaSessionCallback.commandManager.updateFavoriteState(mediaSession, favorited)
|
|
934
934
|
}
|
|
935
935
|
|
|
936
|
+
internal fun updateLiveState(live: Boolean) {
|
|
937
|
+
if (!::mediaSession.isInitialized) return
|
|
938
|
+
mediaSessionCallback.commandManager.updateLiveState(mediaSession, live)
|
|
939
|
+
}
|
|
940
|
+
|
|
936
941
|
/** Removes all the upcoming tracks, if any (the ones returned by [next]). */
|
|
937
942
|
fun removeUpcomingTracks() {
|
|
938
943
|
val index = exoPlayer.currentMediaItemIndex
|
|
@@ -116,6 +116,7 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
116
116
|
|
|
117
117
|
// Update favorite button state for new track
|
|
118
118
|
player.updateFavoriteButtonState(player.currentTrack?.favorited)
|
|
119
|
+
player.updateLiveState(player.currentTrack?.live == true)
|
|
119
120
|
|
|
120
121
|
// Clear now playing override when track changes (new track = clean slate)
|
|
121
122
|
player.nowPlaying.clearOverride()
|
|
@@ -351,6 +352,9 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
351
352
|
// Only emit for playlist changes, not initial load or other reasons
|
|
352
353
|
if (reason == MediaPlayer.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED) {
|
|
353
354
|
player.callbacks?.onPlaybackQueueChanged(player.tracks)
|
|
355
|
+
// Replacing the current item with one of the same URL updates it in place, without a
|
|
356
|
+
// transition, so a changed `live` arrives only here.
|
|
357
|
+
player.updateLiveState(player.currentTrack?.live == true)
|
|
354
358
|
}
|
|
355
359
|
}
|
|
356
360
|
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
package com.audiobrowser.player
|
|
2
|
+
|
|
3
|
+
import android.os.Looper
|
|
4
|
+
import androidx.media3.common.ForwardingPlayer
|
|
5
|
+
import androidx.media3.common.Player as MediaPlayer
|
|
6
|
+
import androidx.media3.exoplayer.ExoPlayer
|
|
7
|
+
import androidx.media3.session.MediaController
|
|
8
|
+
import androidx.media3.session.MediaSession
|
|
9
|
+
import com.margelo.nitro.audiobrowser.PlayerCapabilities
|
|
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.RobolectricTestRunner
|
|
17
|
+
import org.robolectric.RuntimeEnvironment
|
|
18
|
+
import org.robolectric.Shadows.shadowOf
|
|
19
|
+
|
|
20
|
+
// Robolectric: a MediaSession needs the framework.
|
|
21
|
+
@RunWith(RobolectricTestRunner::class)
|
|
22
|
+
class LiveSeekCommandTest {
|
|
23
|
+
|
|
24
|
+
private val app = RuntimeEnvironment.getApplication()
|
|
25
|
+
private val exoPlayer = ExoPlayer.Builder(app).build()
|
|
26
|
+
// A controller sees the session's grant intersected with the player's own commands, and an empty
|
|
27
|
+
// player cannot seek, so this one offers every command to leave only the session's grant.
|
|
28
|
+
private val session =
|
|
29
|
+
MediaSession.Builder(
|
|
30
|
+
app,
|
|
31
|
+
object : ForwardingPlayer(exoPlayer) {
|
|
32
|
+
override fun getAvailableCommands() =
|
|
33
|
+
MediaPlayer.Commands.Builder().addAllCommands().build()
|
|
34
|
+
|
|
35
|
+
override fun isCommandAvailable(command: Int) = true
|
|
36
|
+
},
|
|
37
|
+
)
|
|
38
|
+
.build()
|
|
39
|
+
private val manager = MediaSessionCommandManager()
|
|
40
|
+
|
|
41
|
+
@After
|
|
42
|
+
fun release() {
|
|
43
|
+
session.release()
|
|
44
|
+
exoPlayer.release()
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private fun capabilities(seekTo: Boolean? = null) =
|
|
48
|
+
PlayerCapabilities(
|
|
49
|
+
play = null,
|
|
50
|
+
pause = null,
|
|
51
|
+
stop = null,
|
|
52
|
+
seekTo = seekTo,
|
|
53
|
+
skipToNext = null,
|
|
54
|
+
skipToPrevious = null,
|
|
55
|
+
jumpForward = null,
|
|
56
|
+
jumpBackward = null,
|
|
57
|
+
favorite = null,
|
|
58
|
+
shuffleMode = null,
|
|
59
|
+
repeatMode = null,
|
|
60
|
+
playbackRate = null,
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
private fun update(capabilities: PlayerCapabilities) =
|
|
64
|
+
manager.updateMediaSession(
|
|
65
|
+
session,
|
|
66
|
+
capabilities,
|
|
67
|
+
remoteButtonLayout = null,
|
|
68
|
+
searchAvailable = false,
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
private fun offersSeek(): Boolean =
|
|
72
|
+
manager
|
|
73
|
+
.buildConnectionResult(session)
|
|
74
|
+
.availablePlayerCommands
|
|
75
|
+
.contains(MediaPlayer.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM)
|
|
76
|
+
|
|
77
|
+
@Test
|
|
78
|
+
fun `a live item cannot be seeked, though seekTo is enabled`() {
|
|
79
|
+
update(capabilities(seekTo = true))
|
|
80
|
+
manager.updateLiveState(session, live = true)
|
|
81
|
+
assertFalse(offersSeek())
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
@Test
|
|
85
|
+
fun `seeking returns when the next item is not live`() {
|
|
86
|
+
update(capabilities())
|
|
87
|
+
manager.updateLiveState(session, live = true)
|
|
88
|
+
manager.updateLiveState(session, live = false)
|
|
89
|
+
assertTrue(offersSeek())
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
@Test
|
|
93
|
+
fun `a capability update while live keeps seeking withheld`() {
|
|
94
|
+
manager.updateLiveState(session, live = true)
|
|
95
|
+
update(capabilities(seekTo = true))
|
|
96
|
+
assertFalse(offersSeek())
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
@Test
|
|
100
|
+
fun `a disabled seekTo stays disabled for an item that is not live`() {
|
|
101
|
+
update(capabilities(seekTo = false))
|
|
102
|
+
manager.updateLiveState(session, live = true)
|
|
103
|
+
manager.updateLiveState(session, live = false)
|
|
104
|
+
assertFalse(offersSeek())
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
@Test
|
|
108
|
+
fun `being live takes only seeking away`() {
|
|
109
|
+
update(capabilities())
|
|
110
|
+
val notLive = manager.buildConnectionResult(session).availablePlayerCommands
|
|
111
|
+
manager.updateLiveState(session, live = true)
|
|
112
|
+
val live = manager.buildConnectionResult(session).availablePlayerCommands
|
|
113
|
+
assertEquals(
|
|
114
|
+
notLive.buildUpon().remove(MediaPlayer.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM).build(),
|
|
115
|
+
live,
|
|
116
|
+
)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
@Test
|
|
120
|
+
fun `a controller already connected loses and regains seeking`() {
|
|
121
|
+
val future = MediaController.Builder(app, session.token).buildAsync()
|
|
122
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
123
|
+
val controller = future.get()
|
|
124
|
+
fun controllerOffersSeek(): Boolean {
|
|
125
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
126
|
+
return controller.isCommandAvailable(MediaPlayer.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
try {
|
|
130
|
+
manager.updateLiveState(session, live = true)
|
|
131
|
+
assertFalse(controllerOffersSeek())
|
|
132
|
+
manager.updateLiveState(session, live = false)
|
|
133
|
+
assertTrue(controllerOffersSeek())
|
|
134
|
+
} finally {
|
|
135
|
+
controller.release()
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -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
|
)
|