react-native-audio-browser 0.2.0-next.85 → 0.2.0-next.86
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.
|
@@ -5,6 +5,7 @@ import android.app.SearchManager
|
|
|
5
5
|
import android.content.ComponentName
|
|
6
6
|
import android.content.Intent
|
|
7
7
|
import android.content.ServiceConnection
|
|
8
|
+
import android.content.pm.PackageManager
|
|
8
9
|
import android.os.Binder
|
|
9
10
|
import android.os.IBinder
|
|
10
11
|
import android.provider.MediaStore
|
|
@@ -152,6 +153,7 @@ class Service : MediaLibraryService(), MediaSessionService.Listener {
|
|
|
152
153
|
mediaSession =
|
|
153
154
|
MediaLibrarySession.Builder(this, player.forwardingPlayer, player.getMediaSessionCallback())
|
|
154
155
|
.setBitmapLoader(CacheBitmapLoader(coilBitmapLoader))
|
|
156
|
+
.setPeriodicPositionUpdateEnabled(periodicPositionUpdatesEnabled())
|
|
155
157
|
// https://github.com/androidx/media/issues/1218
|
|
156
158
|
.setSessionActivity(
|
|
157
159
|
PendingIntent.getActivity(
|
|
@@ -173,6 +175,13 @@ class Service : MediaLibraryService(), MediaSessionService.Listener {
|
|
|
173
175
|
}
|
|
174
176
|
}
|
|
175
177
|
|
|
178
|
+
/** The app's [PERIODIC_POSITION_UPDATES] manifest meta-data; true when it declares none. */
|
|
179
|
+
private fun periodicPositionUpdatesEnabled(): Boolean =
|
|
180
|
+
packageManager
|
|
181
|
+
.getApplicationInfo(packageName, PackageManager.GET_META_DATA)
|
|
182
|
+
.metaData
|
|
183
|
+
?.getBoolean(PERIODIC_POSITION_UPDATES, true) ?: true
|
|
184
|
+
|
|
176
185
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
|
177
186
|
Timber.d("onStartCommand: action=${intent?.action}")
|
|
178
187
|
|
|
@@ -360,4 +369,12 @@ class Service : MediaLibraryService(), MediaSessionService.Listener {
|
|
|
360
369
|
inner class LocalBinder : Binder() {
|
|
361
370
|
val service = this@Service
|
|
362
371
|
}
|
|
372
|
+
|
|
373
|
+
companion object {
|
|
374
|
+
/**
|
|
375
|
+
* Application meta-data that, set to false, stops media3's periodic position update: the
|
|
376
|
+
* session re-sending External Surfaces the playback state every 3 s while playing or loading.
|
|
377
|
+
*/
|
|
378
|
+
const val PERIODIC_POSITION_UPDATES = "com.audiobrowser.PERIODIC_POSITION_UPDATES"
|
|
379
|
+
}
|
|
363
380
|
}
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
package androidx.media3.session
|
|
2
2
|
|
|
3
|
+
import android.os.Handler
|
|
4
|
+
import android.os.Looper
|
|
5
|
+
import androidx.media3.session.legacy.MediaControllerCompat
|
|
6
|
+
import androidx.media3.session.legacy.PlaybackStateCompat
|
|
7
|
+
|
|
3
8
|
// Read in place: Robolectric can't connect a controller to the session.
|
|
4
9
|
|
|
5
10
|
/** The state legacy controllers (Android Auto, Bluetooth, the system UI) were last told. */
|
|
@@ -17,3 +22,17 @@ internal fun MediaSession.controllerError() = impl.playerInfo.playerError
|
|
|
17
22
|
|
|
18
23
|
/** Whether media3 controllers were last told it plays. */
|
|
19
24
|
internal fun MediaSession.controllerPlaying() = impl.playerInfo.isPlaying
|
|
25
|
+
|
|
26
|
+
/** Counts the playback states legacy controllers are told from now on. */
|
|
27
|
+
internal fun MediaSession.countLegacyStateUpdates(): () -> Int {
|
|
28
|
+
var count = 0
|
|
29
|
+
impl.mediaSessionLegacyStub.sessionCompat.controller.registerCallback(
|
|
30
|
+
object : MediaControllerCompat.Callback() {
|
|
31
|
+
override fun onPlaybackStateChanged(state: PlaybackStateCompat?) {
|
|
32
|
+
count += 1
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
Handler(Looper.getMainLooper()),
|
|
36
|
+
)
|
|
37
|
+
return { count }
|
|
38
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
package com.audiobrowser
|
|
2
|
+
|
|
3
|
+
import android.content.ComponentName
|
|
4
|
+
import android.net.Uri
|
|
5
|
+
import android.os.Binder
|
|
6
|
+
import android.os.Bundle
|
|
7
|
+
import android.os.Looper
|
|
8
|
+
import android.os.Process
|
|
9
|
+
import androidx.annotation.OptIn
|
|
10
|
+
import androidx.media3.common.C
|
|
11
|
+
import androidx.media3.common.util.UnstableApi
|
|
12
|
+
import androidx.media3.session.MediaSession
|
|
13
|
+
import androidx.media3.session.countLegacyStateUpdates
|
|
14
|
+
import com.audiobrowser.util.TrackFactory
|
|
15
|
+
import java.io.File
|
|
16
|
+
import java.nio.ByteBuffer
|
|
17
|
+
import java.nio.ByteOrder
|
|
18
|
+
import java.time.Duration
|
|
19
|
+
import org.junit.After
|
|
20
|
+
import org.junit.Assert.assertEquals
|
|
21
|
+
import org.junit.Assert.assertTrue
|
|
22
|
+
import org.junit.Assert.fail
|
|
23
|
+
import org.junit.Rule
|
|
24
|
+
import org.junit.Test
|
|
25
|
+
import org.junit.rules.TemporaryFolder
|
|
26
|
+
import org.junit.runner.RunWith
|
|
27
|
+
import org.robolectric.Robolectric
|
|
28
|
+
import org.robolectric.RobolectricTestRunner
|
|
29
|
+
import org.robolectric.RuntimeEnvironment
|
|
30
|
+
import org.robolectric.Shadows.shadowOf
|
|
31
|
+
import org.robolectric.android.controller.ServiceController
|
|
32
|
+
import org.robolectric.annotation.Config
|
|
33
|
+
import org.robolectric.shadows.ShadowSystemClock
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* What the service's session tells External Surfaces while playback runs with nothing changing,
|
|
37
|
+
* with and without the app's [Service.PERIODIC_POSITION_UPDATES] meta-data.
|
|
38
|
+
*/
|
|
39
|
+
@RunWith(RobolectricTestRunner::class)
|
|
40
|
+
@Config(sdk = [28])
|
|
41
|
+
class SessionPositionUpdatesTest {
|
|
42
|
+
|
|
43
|
+
@get:Rule val folder = TemporaryFolder()
|
|
44
|
+
|
|
45
|
+
private val application = RuntimeEnvironment.getApplication()
|
|
46
|
+
private lateinit var controller: ServiceController<Service>
|
|
47
|
+
private val service
|
|
48
|
+
get() = controller.get()
|
|
49
|
+
|
|
50
|
+
private val player
|
|
51
|
+
get() = service.player
|
|
52
|
+
|
|
53
|
+
@After
|
|
54
|
+
fun destroy() {
|
|
55
|
+
controller.destroy()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
@Test
|
|
59
|
+
fun `by default the session re-sends the playback state every 3 s`() {
|
|
60
|
+
startService(metaData = null)
|
|
61
|
+
|
|
62
|
+
assertEquals(10, legacyStateUpdatesOverSteadyPlayback(Duration.ofSeconds(30)))
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
@Test
|
|
66
|
+
fun `the app's meta-data stops the periodic playback state updates`() {
|
|
67
|
+
startService(metaData = Bundle().apply { putBoolean(Service.PERIODIC_POSITION_UPDATES, false) })
|
|
68
|
+
|
|
69
|
+
assertEquals(0, legacyStateUpdatesOverSteadyPlayback(Duration.ofSeconds(30)))
|
|
70
|
+
|
|
71
|
+
// The counter hears a real change.
|
|
72
|
+
val updates = session().countLegacyStateUpdates()
|
|
73
|
+
player.pause()
|
|
74
|
+
advance(Duration.ofMillis(500))
|
|
75
|
+
assertTrue(updates() > 0)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private fun startService(metaData: Bundle?) {
|
|
79
|
+
shadowOf(application.packageManager)
|
|
80
|
+
.getInternalMutablePackageInfo(application.packageName)
|
|
81
|
+
.applicationInfo!!
|
|
82
|
+
.metaData = metaData
|
|
83
|
+
// Robolectric connects an unregistered binding with a null ComponentName, which the
|
|
84
|
+
// HeadlessTaskService connection's non-null parameter rejects.
|
|
85
|
+
shadowOf(application)
|
|
86
|
+
.setComponentNameAndServiceForBindService(
|
|
87
|
+
ComponentName(application, HeadlessTaskService::class.java),
|
|
88
|
+
Binder(),
|
|
89
|
+
)
|
|
90
|
+
controller = Robolectric.buildService(Service::class.java).create()
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Plays a silent WAV, then counts the legacy playback states sent over [duration]. */
|
|
94
|
+
private fun legacyStateUpdatesOverSteadyPlayback(duration: Duration): Int {
|
|
95
|
+
val session = session()
|
|
96
|
+
player.exoPlayer.trackSelectionParameters =
|
|
97
|
+
player.exoPlayer.trackSelectionParameters
|
|
98
|
+
.buildUpon()
|
|
99
|
+
.setTrackTypeDisabled(C.TRACK_TYPE_AUDIO, true)
|
|
100
|
+
.build()
|
|
101
|
+
goOnline(application)
|
|
102
|
+
val src = Uri.fromFile(silentWav(seconds = 45)).toString()
|
|
103
|
+
player.exoPlayer.setMediaItems(listOf(TrackFactory.toMedia3(TestFixtures.track(src = src))))
|
|
104
|
+
player.exoPlayer.prepare()
|
|
105
|
+
player.play()
|
|
106
|
+
advanceUntil("playing") { player.exoPlayer.isPlaying }
|
|
107
|
+
// Lets the updates that starting playback sends settle.
|
|
108
|
+
advance(Duration.ofSeconds(1))
|
|
109
|
+
|
|
110
|
+
val updates = session.countLegacyStateUpdates()
|
|
111
|
+
repeat((duration.toMillis() / 500).toInt()) { advance(Duration.ofMillis(500)) }
|
|
112
|
+
|
|
113
|
+
assertTrue(player.exoPlayer.isPlaying)
|
|
114
|
+
return updates()
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
@OptIn(UnstableApi::class)
|
|
118
|
+
private fun session(): MediaSession {
|
|
119
|
+
// onGetSession ignores its argument.
|
|
120
|
+
return service.onGetSession(
|
|
121
|
+
MediaSession.ControllerInfo.createTestOnlyControllerInfo(
|
|
122
|
+
application.packageName,
|
|
123
|
+
0,
|
|
124
|
+
Process.myUid(),
|
|
125
|
+
0,
|
|
126
|
+
0,
|
|
127
|
+
false,
|
|
128
|
+
Bundle.EMPTY,
|
|
129
|
+
false,
|
|
130
|
+
)
|
|
131
|
+
)!!
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
private fun advance(duration: Duration) {
|
|
135
|
+
ShadowSystemClock.advanceBy(duration)
|
|
136
|
+
Thread.sleep(10)
|
|
137
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
private fun advanceUntil(what: String, condition: () -> Boolean) {
|
|
141
|
+
val deadline = System.currentTimeMillis() + 5_000
|
|
142
|
+
while (!condition()) {
|
|
143
|
+
if (System.currentTimeMillis() > deadline)
|
|
144
|
+
fail(
|
|
145
|
+
"timed out waiting for $what: ${player.playbackState}, exo ${player.exoPlayer.playbackState}"
|
|
146
|
+
)
|
|
147
|
+
advance(Duration.ofMillis(100))
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** 8 kHz mono 8-bit silence. */
|
|
152
|
+
private fun silentWav(seconds: Int): File {
|
|
153
|
+
val samples = 8_000 * seconds
|
|
154
|
+
val header =
|
|
155
|
+
ByteBuffer.allocate(44).order(ByteOrder.LITTLE_ENDIAN).apply {
|
|
156
|
+
put("RIFF".toByteArray())
|
|
157
|
+
putInt(36 + samples)
|
|
158
|
+
put("WAVEfmt ".toByteArray())
|
|
159
|
+
putInt(16)
|
|
160
|
+
putShort(1)
|
|
161
|
+
putShort(1)
|
|
162
|
+
putInt(8_000)
|
|
163
|
+
putInt(8_000)
|
|
164
|
+
putShort(1)
|
|
165
|
+
putShort(8)
|
|
166
|
+
put("data".toByteArray())
|
|
167
|
+
putInt(samples)
|
|
168
|
+
}
|
|
169
|
+
return folder.newFile("silence.wav").apply {
|
|
170
|
+
writeBytes(header.array() + ByteArray(samples) { 0x80.toByte() })
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
---
|
|
2
|
-
description:
|
|
2
|
+
description: "The map for live audio: stream formats, missing durations, dropped connections, expiring URLs, song titles carried in-band, and Android's periodic position updates."
|
|
3
3
|
---
|
|
4
4
|
|
|
5
5
|
# Live Streams
|
|
@@ -144,13 +144,46 @@ resolved URL, so an expired one re-resolves only after that first attempt
|
|
|
144
144
|
fails (see [Fresh URLs on reconnect](#fresh-urls-on-reconnect)).
|
|
145
145
|
General seeking is in [Playback](/guide/playback).
|
|
146
146
|
|
|
147
|
+
## Periodic position updates (Android)
|
|
148
|
+
|
|
149
|
+
While the player plays or loads, Media3's media session sends External
|
|
150
|
+
Surfaces the playback state again every 3 seconds: the notification, the lock
|
|
151
|
+
screen, Android Auto, Bluetooth, and Media3 controllers in other apps (a
|
|
152
|
+
watch, say). The update carries the current and buffered position, so a
|
|
153
|
+
surface that extrapolates the position corrects any drift.
|
|
154
|
+
|
|
155
|
+
A live track has no progress bar, so a drifting position never shows, and the
|
|
156
|
+
update only refreshes the state's timestamp. Android 16's Bluetooth service
|
|
157
|
+
logs each one as `tried to update with no new data`.
|
|
158
|
+
|
|
159
|
+
If your app plays only live streams, turn the update off with a literal
|
|
160
|
+
`false` in your app's manifest:
|
|
161
|
+
|
|
162
|
+
```xml
|
|
163
|
+
<!-- android/app/src/main/AndroidManifest.xml, inside <application> -->
|
|
164
|
+
<meta-data
|
|
165
|
+
android:name="com.audiobrowser.PERIODIC_POSITION_UPDATES"
|
|
166
|
+
android:value="false" />
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Every real change still reaches the surfaces at once: state, play and pause,
|
|
170
|
+
seeks, speed, Now Playing and the queue. Surfaces extrapolate an on-demand
|
|
171
|
+
position from the last state, so their progress bars keep moving. What stops
|
|
172
|
+
is the correction: a position that drifts with no state change (a Sonos
|
|
173
|
+
speaker that slips while it still reports playing, say) stays wrong on the
|
|
174
|
+
surfaces until the next change.
|
|
175
|
+
|
|
176
|
+
Leave it on, the default, if your app plays on-demand audio. The flag is
|
|
177
|
+
Android-only: iOS and web have no such update.
|
|
178
|
+
|
|
147
179
|
## API summary
|
|
148
180
|
|
|
149
|
-
| API
|
|
150
|
-
|
|
|
151
|
-
| [`Track.live`](/api/types/browser-nodes/#live)
|
|
152
|
-
| [`seekToLiveEdge()`](/api/features/playback/#seektoliveedge)
|
|
153
|
-
| [`setupPlayer({ retry })`](/guide/errors#automatic-retry)
|
|
154
|
-
| [`retry()`](/guide/errors#manual-retry)
|
|
155
|
-
| [`media.resolve`](/guide/browser#media-and-artwork)
|
|
156
|
-
| [`useOnline()`](/guide/network)
|
|
181
|
+
| API | Purpose |
|
|
182
|
+
| ----------------------------------------------------------------- | ----------------------------------------------------------------------- |
|
|
183
|
+
| [`Track.live`](/api/types/browser-nodes/#live) | Declare a live track: iOS live indicator, arms the live edge. |
|
|
184
|
+
| [`seekToLiveEdge()`](/api/features/playback/#seektoliveedge) | Jump to the newest audio (no-op for non-live tracks). |
|
|
185
|
+
| [`setupPlayer({ retry })`](/guide/errors#automatic-retry) | Automatic retry with first-connect and recovery budgets. |
|
|
186
|
+
| [`retry()`](/guide/errors#manual-retry) | Restart from `'error'` with fresh budgets. |
|
|
187
|
+
| [`media.resolve`](/guide/browser#media-and-artwork) | Build the stream request — signed / short-lived URLs. |
|
|
188
|
+
| [`useOnline()`](/guide/network) | Connectivity for your own UI; playback reacts on its own. |
|
|
189
|
+
| [`PERIODIC_POSITION_UPDATES`](#periodic-position-updates-android) | Android manifest flag: stop the 3 s position update for live-only apps. |
|