react-native-audio-browser 0.2.0-next.41 → 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/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/player/LiveSeekCommandTest.kt +138 -0
- package/package.json +1 -1
|
@@ -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,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
|
+
}
|