react-native-audio-browser 0.2.0-next.39 → 0.2.0-next.40
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/NowPlayingUpdater.kt +2 -1
- package/android/src/main/java/com/audiobrowser/player/PlaybackStateMachine.kt +26 -14
- package/android/src/main/java/com/audiobrowser/player/Player.kt +20 -10
- package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +11 -3
- package/android/src/main/java/com/audiobrowser/util/PlayingStateFactory.kt +6 -5
- package/android/src/test/java/com/audiobrowser/player/PlaybackStateMachineTest.kt +48 -16
- package/android/src/test/java/com/audiobrowser/player/PlayerSuppressionTest.kt +185 -0
- package/android/src/test/java/com/audiobrowser/util/PlayingStateFactoryTest.kt +6 -6
- package/package.json +1 -1
|
@@ -30,7 +30,8 @@ interface NowPlayingSurface {
|
|
|
30
30
|
val playbackError: PlaybackError?
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
|
-
* MUST be the real play/pause intent read from the underlying ExoPlayer
|
|
33
|
+
* MUST be the real play/pause intent read from the underlying ExoPlayer, false while a transient
|
|
34
|
+
* audio focus loss suppresses output (as iOS pauses on an interruption) — not the
|
|
34
35
|
* InterceptingPlayer-masked value, which can report false through a masked terminal error to keep
|
|
35
36
|
* the session paused-but-alive. The formatter receives this; feeding it the masked value breaks
|
|
36
37
|
* its output exactly in the error case the mask exists for.
|
|
@@ -17,13 +17,21 @@ sealed interface PlaybackEvent {
|
|
|
17
17
|
data class ExoPlaybackStateChanged(
|
|
18
18
|
@get:MediaPlayer.State val exoState: Int,
|
|
19
19
|
val mediaItemCount: Int,
|
|
20
|
-
val
|
|
20
|
+
val shouldPlayWhenReady: Boolean,
|
|
21
21
|
) : PlaybackEvent
|
|
22
22
|
|
|
23
23
|
/** The active media item changed (auto-advance, skip, queue swap). */
|
|
24
24
|
data class MediaItemTransition(val hasTrack: Boolean, val isPlaying: Boolean) : PlaybackEvent
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
/**
|
|
27
|
+
* An input of [Player.shouldPlayWhenReady] changed: the intent, or a playback suppression such as
|
|
28
|
+
* a transient audio focus loss, which moves the machine as a pause does. [exoState] is the real
|
|
29
|
+
* ExoPlayer state, as in [ExoPlaybackStateChanged].
|
|
30
|
+
*/
|
|
31
|
+
data class ShouldPlayWhenReadyChanged(
|
|
32
|
+
val shouldPlayWhenReady: Boolean,
|
|
33
|
+
@get:MediaPlayer.State val exoState: Int,
|
|
34
|
+
) : PlaybackEvent
|
|
27
35
|
|
|
28
36
|
data class IsPlayingChanged(val isPlaying: Boolean) : PlaybackEvent
|
|
29
37
|
}
|
|
@@ -42,12 +50,12 @@ object PlaybackStateMachine {
|
|
|
42
50
|
when (event.exoState) {
|
|
43
51
|
MediaPlayer.STATE_BUFFERING -> listOf(PlaybackState.BUFFERING)
|
|
44
52
|
MediaPlayer.STATE_READY ->
|
|
45
|
-
// While playing (
|
|
53
|
+
// While playing (shouldPlayWhenReady), READY is a transient before PLAYING —
|
|
46
54
|
// suppress it so consumers don't flash a settled/non-loading state
|
|
47
55
|
// mid-startup (none→loading→buffering→playing). When paused it IS the
|
|
48
56
|
// settled signal a buffer-that-finished-while-paused needs, so consumers
|
|
49
57
|
// can clear their loading spinner (mono#3325).
|
|
50
|
-
if (event.
|
|
58
|
+
if (event.shouldPlayWhenReady) emptyList() else listOf(PlaybackState.READY)
|
|
51
59
|
MediaPlayer.STATE_IDLE ->
|
|
52
60
|
// A terminal error (or an explicit stop) idles ExoPlayer; transitioning to NONE
|
|
53
61
|
// would clear the ERROR/STOPPED state the session is rendering.
|
|
@@ -68,16 +76,20 @@ object PlaybackStateMachine {
|
|
|
68
76
|
listOf(PlaybackState.LOADING, PlaybackState.READY, PlaybackState.PLAYING)
|
|
69
77
|
else -> listOf(PlaybackState.LOADING)
|
|
70
78
|
}
|
|
71
|
-
is PlaybackEvent.
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
!event.
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
79
|
+
is PlaybackEvent.ShouldPlayWhenReadyChanged ->
|
|
80
|
+
when {
|
|
81
|
+
// STOPPED and ENDED are terminal: dropping the intent there (stop()'s own
|
|
82
|
+
// clear, the clear on natural end, or a redundant pause press) must not
|
|
83
|
+
// re-report the state as PAUSED.
|
|
84
|
+
!event.shouldPlayWhenReady &&
|
|
85
|
+
current != PlaybackState.STOPPED &&
|
|
86
|
+
current != PlaybackState.ENDED -> listOf(PlaybackState.PAUSED)
|
|
87
|
+
!event.shouldPlayWhenReady -> emptyList()
|
|
88
|
+
// Resuming while ExoPlayer still buffers (a pause or suppression that began during a
|
|
89
|
+
// load): READY will be dropped as the transient before PLAYING, so report the load now.
|
|
90
|
+
current == PlaybackState.PAUSED && event.exoState == MediaPlayer.STATE_BUFFERING ->
|
|
91
|
+
listOf(PlaybackState.BUFFERING)
|
|
92
|
+
else -> emptyList()
|
|
81
93
|
}
|
|
82
94
|
is PlaybackEvent.IsPlayingChanged ->
|
|
83
95
|
if (event.isPlaying) listOf(PlaybackState.PLAYING) else emptyList()
|
|
@@ -257,13 +257,12 @@ class Player(internal val context: Context) {
|
|
|
257
257
|
}
|
|
258
258
|
|
|
259
259
|
/**
|
|
260
|
-
* Re-derives [playingState] from the current
|
|
261
|
-
* `onPlaybackPlayingState` when it changed. The only writer of [playingState]
|
|
262
|
-
*
|
|
263
|
-
* points of its two inputs.
|
|
260
|
+
* Re-derives [playingState] from the current [shouldPlayWhenReady] + [playbackState] and emits
|
|
261
|
+
* `onPlaybackPlayingState` when it changed. The only writer of [playingState], called at every
|
|
262
|
+
* change point of its inputs.
|
|
264
263
|
*/
|
|
265
264
|
internal fun refreshPlayingState() {
|
|
266
|
-
val newPlayingState = PlayingStateFactory.derive(
|
|
265
|
+
val newPlayingState = PlayingStateFactory.derive(shouldPlayWhenReady, playbackState)
|
|
267
266
|
if (newPlayingState == playingState) return
|
|
268
267
|
Timber.d(
|
|
269
268
|
"PlayingState changed: playing=${newPlayingState.playing}, buffering=${newPlayingState.buffering}"
|
|
@@ -315,6 +314,15 @@ class Player(internal val context: Context) {
|
|
|
315
314
|
val isPlaying
|
|
316
315
|
get() = exoPlayer.isPlaying
|
|
317
316
|
|
|
317
|
+
/**
|
|
318
|
+
* False while ExoPlayer suppresses output (a transient audio focus loss) though [playWhenReady]
|
|
319
|
+
* stays raised for it to resume on; mirrors media3's `ExoPlayerImplInternal.shouldPlayWhenReady`.
|
|
320
|
+
*/
|
|
321
|
+
internal val shouldPlayWhenReady
|
|
322
|
+
get() =
|
|
323
|
+
exoPlayer.playWhenReady &&
|
|
324
|
+
exoPlayer.playbackSuppressionReason == ExoPlayer.PLAYBACK_SUPPRESSION_REASON_NONE
|
|
325
|
+
|
|
318
326
|
var repeatMode: RepeatMode
|
|
319
327
|
get() = RepeatModeFactory.fromMedia3(exoPlayer.repeatMode)
|
|
320
328
|
internal set(value) {
|
|
@@ -788,7 +796,7 @@ class Player(internal val context: Context) {
|
|
|
788
796
|
get() = this@Player.playbackError
|
|
789
797
|
|
|
790
798
|
override val playWhenReady
|
|
791
|
-
get() =
|
|
799
|
+
get() = shouldPlayWhenReady
|
|
792
800
|
|
|
793
801
|
override val isRebuffering
|
|
794
802
|
get() = loadControl.isRebuffering
|
|
@@ -1026,10 +1034,12 @@ class Player(internal val context: Context) {
|
|
|
1026
1034
|
}
|
|
1027
1035
|
|
|
1028
1036
|
fun togglePlayback() {
|
|
1029
|
-
|
|
1030
|
-
pause()
|
|
1031
|
-
|
|
1032
|
-
|
|
1037
|
+
when {
|
|
1038
|
+
shouldPlayWhenReady -> pause()
|
|
1039
|
+
// Suppressed with the intent raised: ExoPlayer resumes when focus returns. play() would
|
|
1040
|
+
// request focus again, and a call refuses it, which drops the intent.
|
|
1041
|
+
playWhenReady -> Unit
|
|
1042
|
+
else -> play()
|
|
1033
1043
|
}
|
|
1034
1044
|
}
|
|
1035
1045
|
|
|
@@ -186,6 +186,10 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
186
186
|
}
|
|
187
187
|
}
|
|
188
188
|
|
|
189
|
+
override fun onPlaybackSuppressionReasonChanged(playbackSuppressionReason: Int) {
|
|
190
|
+
player.refreshPlayingState()
|
|
191
|
+
}
|
|
192
|
+
|
|
189
193
|
/**
|
|
190
194
|
* The generic onEvents callback provides access to the Player object and specifies the set of
|
|
191
195
|
* events that occurred together. It's always called after the callbacks that correspond to the
|
|
@@ -211,12 +215,16 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
211
215
|
PlaybackEvent.ExoPlaybackStateChanged(
|
|
212
216
|
player.exoPlayer.playbackState,
|
|
213
217
|
media3Player.mediaItemCount,
|
|
214
|
-
player.
|
|
218
|
+
player.shouldPlayWhenReady,
|
|
215
219
|
)
|
|
216
220
|
MediaPlayer.EVENT_MEDIA_ITEM_TRANSITION ->
|
|
217
221
|
PlaybackEvent.MediaItemTransition(player.currentTrack != null, player.isPlaying)
|
|
218
|
-
MediaPlayer.EVENT_PLAY_WHEN_READY_CHANGED
|
|
219
|
-
|
|
222
|
+
MediaPlayer.EVENT_PLAY_WHEN_READY_CHANGED,
|
|
223
|
+
MediaPlayer.EVENT_PLAYBACK_SUPPRESSION_REASON_CHANGED ->
|
|
224
|
+
PlaybackEvent.ShouldPlayWhenReadyChanged(
|
|
225
|
+
player.shouldPlayWhenReady,
|
|
226
|
+
player.exoPlayer.playbackState,
|
|
227
|
+
)
|
|
220
228
|
MediaPlayer.EVENT_IS_PLAYING_CHANGED -> PlaybackEvent.IsPlayingChanged(player.isPlaying)
|
|
221
229
|
else -> null
|
|
222
230
|
} ?: continue
|
|
@@ -5,21 +5,22 @@ import com.margelo.nitro.audiobrowser.PlayingState
|
|
|
5
5
|
|
|
6
6
|
object PlayingStateFactory {
|
|
7
7
|
/**
|
|
8
|
-
* Derives a PlayingState from
|
|
8
|
+
* Derives a PlayingState from shouldPlayWhenReady and state.
|
|
9
9
|
*
|
|
10
|
-
* @param
|
|
10
|
+
* @param shouldPlayWhenReady Whether the player wants to play when ready and nothing holds its
|
|
11
|
+
* output back
|
|
11
12
|
* @param playbackState The current player state
|
|
12
13
|
* @return A PlayingState representing the current playing/buffering status
|
|
13
14
|
*/
|
|
14
|
-
fun derive(
|
|
15
|
+
fun derive(shouldPlayWhenReady: Boolean, playbackState: PlaybackState): PlayingState {
|
|
15
16
|
val playing =
|
|
16
|
-
|
|
17
|
+
shouldPlayWhenReady &&
|
|
17
18
|
!(playbackState == PlaybackState.ERROR ||
|
|
18
19
|
playbackState == PlaybackState.ENDED ||
|
|
19
20
|
playbackState == PlaybackState.NONE ||
|
|
20
21
|
playbackState == PlaybackState.STOPPED)
|
|
21
22
|
val buffering =
|
|
22
|
-
|
|
23
|
+
shouldPlayWhenReady &&
|
|
23
24
|
(playbackState == PlaybackState.LOADING || playbackState == PlaybackState.BUFFERING)
|
|
24
25
|
return PlayingState(playing, buffering)
|
|
25
26
|
}
|
|
@@ -27,7 +27,7 @@ class PlaybackStateMachineTest {
|
|
|
27
27
|
PlaybackEvent.ExoPlaybackStateChanged(
|
|
28
28
|
MediaPlayer.STATE_BUFFERING,
|
|
29
29
|
mediaItemCount = 1,
|
|
30
|
-
|
|
30
|
+
shouldPlayWhenReady = true,
|
|
31
31
|
)
|
|
32
32
|
),
|
|
33
33
|
)
|
|
@@ -35,7 +35,7 @@ class PlaybackStateMachineTest {
|
|
|
35
35
|
|
|
36
36
|
@Test
|
|
37
37
|
fun `ready is emitted only while paused, suppressed while playing`() {
|
|
38
|
-
// Paused (
|
|
38
|
+
// Paused (shouldPlayWhenReady=false): READY is the settled signal consumers need to
|
|
39
39
|
// clear a spinner from a buffer that finished while paused (mono#3325).
|
|
40
40
|
assertEquals(
|
|
41
41
|
listOf(PlaybackState.READY),
|
|
@@ -43,12 +43,12 @@ class PlaybackStateMachineTest {
|
|
|
43
43
|
PlaybackEvent.ExoPlaybackStateChanged(
|
|
44
44
|
MediaPlayer.STATE_READY,
|
|
45
45
|
mediaItemCount = 1,
|
|
46
|
-
|
|
46
|
+
shouldPlayWhenReady = false,
|
|
47
47
|
),
|
|
48
48
|
from = PlaybackState.BUFFERING,
|
|
49
49
|
),
|
|
50
50
|
)
|
|
51
|
-
// Playing (
|
|
51
|
+
// Playing (shouldPlayWhenReady=true): READY is a transient before PLAYING — suppressed
|
|
52
52
|
// so consumers don't flash a settled/non-loading state mid-startup.
|
|
53
53
|
assertEquals(
|
|
54
54
|
emptyList<PlaybackState>(),
|
|
@@ -56,7 +56,7 @@ class PlaybackStateMachineTest {
|
|
|
56
56
|
PlaybackEvent.ExoPlaybackStateChanged(
|
|
57
57
|
MediaPlayer.STATE_READY,
|
|
58
58
|
mediaItemCount = 1,
|
|
59
|
-
|
|
59
|
+
shouldPlayWhenReady = true,
|
|
60
60
|
),
|
|
61
61
|
from = PlaybackState.BUFFERING,
|
|
62
62
|
),
|
|
@@ -69,7 +69,7 @@ class PlaybackStateMachineTest {
|
|
|
69
69
|
PlaybackEvent.ExoPlaybackStateChanged(
|
|
70
70
|
MediaPlayer.STATE_IDLE,
|
|
71
71
|
mediaItemCount = 1,
|
|
72
|
-
|
|
72
|
+
shouldPlayWhenReady = false,
|
|
73
73
|
)
|
|
74
74
|
assertEquals(listOf(PlaybackState.NONE), on(idle, from = PlaybackState.PLAYING))
|
|
75
75
|
// A terminal error idles ExoPlayer; transitioning to NONE would clear the ERROR state the
|
|
@@ -86,7 +86,7 @@ class PlaybackStateMachineTest {
|
|
|
86
86
|
PlaybackEvent.ExoPlaybackStateChanged(
|
|
87
87
|
MediaPlayer.STATE_ENDED,
|
|
88
88
|
mediaItemCount = 3,
|
|
89
|
-
|
|
89
|
+
shouldPlayWhenReady = false,
|
|
90
90
|
)
|
|
91
91
|
),
|
|
92
92
|
)
|
|
@@ -97,7 +97,7 @@ class PlaybackStateMachineTest {
|
|
|
97
97
|
PlaybackEvent.ExoPlaybackStateChanged(
|
|
98
98
|
MediaPlayer.STATE_ENDED,
|
|
99
99
|
mediaItemCount = 0,
|
|
100
|
-
|
|
100
|
+
shouldPlayWhenReady = false,
|
|
101
101
|
)
|
|
102
102
|
),
|
|
103
103
|
)
|
|
@@ -114,19 +114,19 @@ class PlaybackStateMachineTest {
|
|
|
114
114
|
PlaybackEvent.ExoPlaybackStateChanged(
|
|
115
115
|
MediaPlayer.STATE_BUFFERING,
|
|
116
116
|
mediaItemCount = 1,
|
|
117
|
-
|
|
117
|
+
shouldPlayWhenReady = true,
|
|
118
118
|
),
|
|
119
119
|
from = PlaybackState.ERROR,
|
|
120
120
|
),
|
|
121
121
|
)
|
|
122
|
-
// A paused recovery reaching READY (
|
|
122
|
+
// A paused recovery reaching READY (shouldPlayWhenReady=false) also leaves ERROR.
|
|
123
123
|
assertEquals(
|
|
124
124
|
listOf(PlaybackState.READY),
|
|
125
125
|
on(
|
|
126
126
|
PlaybackEvent.ExoPlaybackStateChanged(
|
|
127
127
|
MediaPlayer.STATE_READY,
|
|
128
128
|
mediaItemCount = 1,
|
|
129
|
-
|
|
129
|
+
shouldPlayWhenReady = false,
|
|
130
130
|
),
|
|
131
131
|
from = PlaybackState.ERROR,
|
|
132
132
|
),
|
|
@@ -141,7 +141,7 @@ class PlaybackStateMachineTest {
|
|
|
141
141
|
PlaybackEvent.ExoPlaybackStateChanged(
|
|
142
142
|
exoState = 99,
|
|
143
143
|
mediaItemCount = 1,
|
|
144
|
-
|
|
144
|
+
shouldPlayWhenReady = false,
|
|
145
145
|
)
|
|
146
146
|
),
|
|
147
147
|
)
|
|
@@ -171,11 +171,15 @@ class PlaybackStateMachineTest {
|
|
|
171
171
|
)
|
|
172
172
|
}
|
|
173
173
|
|
|
174
|
-
// MARK:
|
|
174
|
+
// MARK: shouldPlayWhenReady / isPlaying
|
|
175
175
|
|
|
176
176
|
@Test
|
|
177
|
-
fun `losing
|
|
178
|
-
val paused =
|
|
177
|
+
fun `losing shouldPlayWhenReady pauses except from stopped or ended`() {
|
|
178
|
+
val paused =
|
|
179
|
+
PlaybackEvent.ShouldPlayWhenReadyChanged(
|
|
180
|
+
shouldPlayWhenReady = false,
|
|
181
|
+
exoState = MediaPlayer.STATE_READY,
|
|
182
|
+
)
|
|
179
183
|
assertEquals(listOf(PlaybackState.PAUSED), on(paused, from = PlaybackState.PLAYING))
|
|
180
184
|
assertEquals(emptyList<PlaybackState>(), on(paused, from = PlaybackState.STOPPED))
|
|
181
185
|
// ENDED is terminal the same way: dropping the intent after a natural end (the
|
|
@@ -183,7 +187,35 @@ class PlaybackStateMachineTest {
|
|
|
183
187
|
assertEquals(emptyList<PlaybackState>(), on(paused, from = PlaybackState.ENDED))
|
|
184
188
|
assertEquals(
|
|
185
189
|
emptyList<PlaybackState>(),
|
|
186
|
-
on(
|
|
190
|
+
on(
|
|
191
|
+
PlaybackEvent.ShouldPlayWhenReadyChanged(
|
|
192
|
+
shouldPlayWhenReady = true,
|
|
193
|
+
exoState = MediaPlayer.STATE_READY,
|
|
194
|
+
)
|
|
195
|
+
),
|
|
196
|
+
)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
@Test
|
|
200
|
+
fun `resuming from paused while exo buffers reports buffering`() {
|
|
201
|
+
val resumed =
|
|
202
|
+
PlaybackEvent.ShouldPlayWhenReadyChanged(
|
|
203
|
+
shouldPlayWhenReady = true,
|
|
204
|
+
exoState = MediaPlayer.STATE_BUFFERING,
|
|
205
|
+
)
|
|
206
|
+
assertEquals(listOf(PlaybackState.BUFFERING), on(resumed, from = PlaybackState.PAUSED))
|
|
207
|
+
assertEquals(emptyList<PlaybackState>(), on(resumed, from = PlaybackState.LOADING))
|
|
208
|
+
assertEquals(emptyList<PlaybackState>(), on(resumed, from = PlaybackState.STOPPED))
|
|
209
|
+
// Exo READY: PLAYING follows from IsPlayingChanged.
|
|
210
|
+
assertEquals(
|
|
211
|
+
emptyList<PlaybackState>(),
|
|
212
|
+
on(
|
|
213
|
+
PlaybackEvent.ShouldPlayWhenReadyChanged(
|
|
214
|
+
shouldPlayWhenReady = true,
|
|
215
|
+
exoState = MediaPlayer.STATE_READY,
|
|
216
|
+
),
|
|
217
|
+
from = PlaybackState.PAUSED,
|
|
218
|
+
),
|
|
187
219
|
)
|
|
188
220
|
}
|
|
189
221
|
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
package com.audiobrowser.player
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.media.AudioManager
|
|
5
|
+
import android.net.Uri
|
|
6
|
+
import android.os.Looper
|
|
7
|
+
import androidx.media3.common.Player as MediaPlayer
|
|
8
|
+
import com.audiobrowser.Callbacks
|
|
9
|
+
import com.audiobrowser.TestFixtures
|
|
10
|
+
import com.audiobrowser.model.PlayerSetupOptions
|
|
11
|
+
import com.audiobrowser.util.TrackFactory
|
|
12
|
+
import com.margelo.nitro.audiobrowser.Playback
|
|
13
|
+
import com.margelo.nitro.audiobrowser.PlaybackState
|
|
14
|
+
import com.margelo.nitro.audiobrowser.PlayingState
|
|
15
|
+
import java.io.File
|
|
16
|
+
import java.nio.ByteBuffer
|
|
17
|
+
import java.nio.ByteOrder
|
|
18
|
+
import org.junit.After
|
|
19
|
+
import org.junit.Assert.assertEquals
|
|
20
|
+
import org.junit.Assert.assertFalse
|
|
21
|
+
import org.junit.Assert.assertTrue
|
|
22
|
+
import org.junit.Assert.fail
|
|
23
|
+
import org.junit.Before
|
|
24
|
+
import org.junit.Rule
|
|
25
|
+
import org.junit.Test
|
|
26
|
+
import org.junit.rules.TemporaryFolder
|
|
27
|
+
import org.junit.runner.RunWith
|
|
28
|
+
import org.mockito.Mockito.clearInvocations
|
|
29
|
+
import org.mockito.Mockito.mock
|
|
30
|
+
import org.mockito.Mockito.mockingDetails
|
|
31
|
+
import org.mockito.Mockito.verify
|
|
32
|
+
import org.robolectric.RobolectricTestRunner
|
|
33
|
+
import org.robolectric.RuntimeEnvironment
|
|
34
|
+
import org.robolectric.Shadows.shadowOf
|
|
35
|
+
import org.robolectric.annotation.Config
|
|
36
|
+
|
|
37
|
+
// Robolectric: Player builds a real ExoPlayer, whose audio focus handling the framework's
|
|
38
|
+
// AudioManager drives. With no decoder under Robolectric, the WAV plays on ExoPlayer's standalone
|
|
39
|
+
// clock, which still reaches READY and isPlaying. SDK 28: from 33, media3 asks
|
|
40
|
+
// AudioManager.getAudioDevicesForAttributes, which Robolectric 4.11 answers with null.
|
|
41
|
+
@RunWith(RobolectricTestRunner::class)
|
|
42
|
+
@Config(sdk = [28])
|
|
43
|
+
class PlayerSuppressionTest {
|
|
44
|
+
|
|
45
|
+
@get:Rule val folder = TemporaryFolder()
|
|
46
|
+
|
|
47
|
+
private val context = RuntimeEnvironment.getApplication()
|
|
48
|
+
private val player = Player(context)
|
|
49
|
+
private val callbacks = mock(Callbacks::class.java)
|
|
50
|
+
private val audioManager =
|
|
51
|
+
shadowOf(context.getSystemService(Context.AUDIO_SERVICE) as AudioManager)
|
|
52
|
+
|
|
53
|
+
@Before
|
|
54
|
+
fun play() {
|
|
55
|
+
player.setup(PlayerSetupOptions())
|
|
56
|
+
player.setCallbacks(callbacks)
|
|
57
|
+
val src = Uri.fromFile(silentWav()).toString()
|
|
58
|
+
player.exoPlayer.setMediaItems(
|
|
59
|
+
listOf(TrackFactory.toMedia3(TestFixtures.track(title = "a", src = src)))
|
|
60
|
+
)
|
|
61
|
+
player.exoPlayer.prepare()
|
|
62
|
+
player.play()
|
|
63
|
+
idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
|
|
64
|
+
clearInvocations(callbacks)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
@After fun release() = player.exoPlayer.release()
|
|
68
|
+
|
|
69
|
+
@Test
|
|
70
|
+
fun `a transient focus loss reads as paused and keeps the intent`() {
|
|
71
|
+
focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
|
|
72
|
+
|
|
73
|
+
assertTrue(player.exoPlayer.playWhenReady)
|
|
74
|
+
assertEquals(PlaybackState.PAUSED, player.playbackState)
|
|
75
|
+
verify(callbacks).onPlaybackPlayingState(PlayingState(false, false))
|
|
76
|
+
verify(callbacks).onPlaybackChanged(Playback(PlaybackState.PAUSED, null))
|
|
77
|
+
assertTrue(
|
|
78
|
+
mockingDetails(callbacks).invocations.none {
|
|
79
|
+
it.method.name == "onPlaybackPlayWhenReadyChanged"
|
|
80
|
+
}
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
@Test
|
|
85
|
+
fun `regaining focus reads as playing again`() {
|
|
86
|
+
focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
|
|
87
|
+
clearInvocations(callbacks)
|
|
88
|
+
|
|
89
|
+
focus(AudioManager.AUDIOFOCUS_GAIN)
|
|
90
|
+
idleUntil("playing again") { player.playbackState == PlaybackState.PLAYING }
|
|
91
|
+
|
|
92
|
+
verify(callbacks).onPlaybackPlayingState(PlayingState(true, false))
|
|
93
|
+
verify(callbacks).onPlaybackChanged(Playback(PlaybackState.PLAYING, null))
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
@Test
|
|
97
|
+
fun `the now playing formatter reads paused during a transient focus loss`() {
|
|
98
|
+
val seen = mutableListOf<Boolean>()
|
|
99
|
+
player.nowPlaying.formatter = { params ->
|
|
100
|
+
seen += params.playWhenReady
|
|
101
|
+
null
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
|
|
105
|
+
assertEquals(false, seen.lastOrNull())
|
|
106
|
+
|
|
107
|
+
focus(AudioManager.AUDIOFOCUS_GAIN)
|
|
108
|
+
idleUntil("playing again") { player.playbackState == PlaybackState.PLAYING }
|
|
109
|
+
assertEquals(true, seen.lastOrNull())
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
@Test
|
|
113
|
+
fun `toggling during a transient focus loss keeps the intent`() {
|
|
114
|
+
focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
|
|
115
|
+
|
|
116
|
+
// As during a call: a request for focus would be refused.
|
|
117
|
+
audioManager.setNextFocusRequestResponse(AudioManager.AUDIOFOCUS_REQUEST_FAILED)
|
|
118
|
+
|
|
119
|
+
player.togglePlayback()
|
|
120
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
121
|
+
assertEquals(PlaybackState.PAUSED, player.playbackState)
|
|
122
|
+
focus(AudioManager.AUDIOFOCUS_GAIN)
|
|
123
|
+
|
|
124
|
+
assertTrue(player.exoPlayer.playWhenReady)
|
|
125
|
+
idleUntil("playing again") { player.playbackState == PlaybackState.PLAYING }
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
@Test
|
|
129
|
+
fun `pausing during a transient focus loss stays paused after it`() {
|
|
130
|
+
focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
|
|
131
|
+
|
|
132
|
+
player.pause()
|
|
133
|
+
focus(AudioManager.AUDIOFOCUS_GAIN)
|
|
134
|
+
|
|
135
|
+
assertFalse(player.exoPlayer.playWhenReady)
|
|
136
|
+
assertEquals(PlaybackState.PAUSED, player.playbackState)
|
|
137
|
+
assertEquals(PlayingState(false, false), player.getPlayingState())
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Delivers [change] and waits for ExoPlayer's playback thread to report the suppression. */
|
|
141
|
+
private fun focus(change: Int) {
|
|
142
|
+
audioManager.lastAudioFocusRequest.listener.onAudioFocusChange(change)
|
|
143
|
+
val suppressed = change == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
|
|
144
|
+
idleUntil("suppressed=$suppressed") {
|
|
145
|
+
(player.exoPlayer.playbackSuppressionReason !=
|
|
146
|
+
MediaPlayer.PLAYBACK_SUPPRESSION_REASON_NONE) == suppressed
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private fun idleUntil(what: String, condition: () -> Boolean) {
|
|
151
|
+
val deadline = System.currentTimeMillis() + 5_000
|
|
152
|
+
while (!condition()) {
|
|
153
|
+
if (System.currentTimeMillis() > deadline)
|
|
154
|
+
fail(
|
|
155
|
+
"timed out waiting for $what: ${player.playbackState}, " +
|
|
156
|
+
"exo ${player.exoPlayer.playbackState}, ${player.exoPlayer.playerError}"
|
|
157
|
+
)
|
|
158
|
+
Thread.sleep(10)
|
|
159
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** A minute of 8 kHz mono 8-bit silence, long enough that no test reaches its end. */
|
|
164
|
+
private fun silentWav(): File {
|
|
165
|
+
val samples = 8_000 * 60
|
|
166
|
+
val header =
|
|
167
|
+
ByteBuffer.allocate(44).order(ByteOrder.LITTLE_ENDIAN).apply {
|
|
168
|
+
put("RIFF".toByteArray())
|
|
169
|
+
putInt(36 + samples)
|
|
170
|
+
put("WAVEfmt ".toByteArray())
|
|
171
|
+
putInt(16)
|
|
172
|
+
putShort(1)
|
|
173
|
+
putShort(1)
|
|
174
|
+
putInt(8_000)
|
|
175
|
+
putInt(8_000)
|
|
176
|
+
putShort(1)
|
|
177
|
+
putShort(8)
|
|
178
|
+
put("data".toByteArray())
|
|
179
|
+
putInt(samples)
|
|
180
|
+
}
|
|
181
|
+
return folder.newFile("silence.wav").apply {
|
|
182
|
+
writeBytes(header.array() + ByteArray(samples) { 0x80.toByte() })
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
@@ -11,29 +11,29 @@ import org.junit.Test
|
|
|
11
11
|
class PlayingStateFactoryTest {
|
|
12
12
|
|
|
13
13
|
@Test
|
|
14
|
-
fun `
|
|
14
|
+
fun `shouldPlayWhenReady false is never playing nor buffering`() {
|
|
15
15
|
for (state in PlaybackState.entries) {
|
|
16
|
-
val derived = PlayingStateFactory.derive(
|
|
16
|
+
val derived = PlayingStateFactory.derive(shouldPlayWhenReady = false, playbackState = state)
|
|
17
17
|
assertEquals("playing for $state", false, derived.playing)
|
|
18
18
|
assertEquals("buffering for $state", false, derived.buffering)
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
@Test
|
|
23
|
-
fun `
|
|
23
|
+
fun `shouldPlayWhenReady true plays except in terminal or empty states`() {
|
|
24
24
|
val notPlaying =
|
|
25
25
|
setOf(PlaybackState.ERROR, PlaybackState.ENDED, PlaybackState.NONE, PlaybackState.STOPPED)
|
|
26
26
|
for (state in PlaybackState.entries) {
|
|
27
|
-
val derived = PlayingStateFactory.derive(
|
|
27
|
+
val derived = PlayingStateFactory.derive(shouldPlayWhenReady = true, playbackState = state)
|
|
28
28
|
assertEquals("playing for $state", state !in notPlaying, derived.playing)
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
@Test
|
|
33
|
-
fun `buffering only while loading or rebuffering with
|
|
33
|
+
fun `buffering only while loading or rebuffering with shouldPlayWhenReady`() {
|
|
34
34
|
val buffering = setOf(PlaybackState.LOADING, PlaybackState.BUFFERING)
|
|
35
35
|
for (state in PlaybackState.entries) {
|
|
36
|
-
val derived = PlayingStateFactory.derive(
|
|
36
|
+
val derived = PlayingStateFactory.derive(shouldPlayWhenReady = true, playbackState = state)
|
|
37
37
|
assertEquals("buffering for $state", state in buffering, derived.buffering)
|
|
38
38
|
}
|
|
39
39
|
}
|