react-native-audio-browser 0.2.0-next.58 → 0.2.0-next.59
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.
|
@@ -66,8 +66,8 @@ class PlaybackStateStore(private val player: Player) {
|
|
|
66
66
|
private var periodicSaveJob: Job? = null
|
|
67
67
|
|
|
68
68
|
/**
|
|
69
|
-
*
|
|
70
|
-
*
|
|
69
|
+
* Calls [save] every [PERIODIC_SAVE_INTERVAL_MS] while playing. A no-op when already running or
|
|
70
|
+
* when the player is not set to play.
|
|
71
71
|
*/
|
|
72
72
|
fun startPeriodicSave() {
|
|
73
73
|
if (periodicSaveJob != null) return
|
|
@@ -87,9 +87,7 @@ class PlaybackStateStore(private val player: Player) {
|
|
|
87
87
|
scope.launch {
|
|
88
88
|
while (true) {
|
|
89
89
|
delay(PERIODIC_SAVE_INTERVAL_MS)
|
|
90
|
-
|
|
91
|
-
savePosition()
|
|
92
|
-
}
|
|
90
|
+
save()
|
|
93
91
|
}
|
|
94
92
|
}
|
|
95
93
|
}
|
|
@@ -103,20 +101,22 @@ class PlaybackStateStore(private val player: Player) {
|
|
|
103
101
|
periodicSaveJob = null
|
|
104
102
|
}
|
|
105
103
|
|
|
106
|
-
/** Stops the periodic save and
|
|
104
|
+
/** Stops the periodic save and calls [save] once more. */
|
|
107
105
|
fun stopPeriodicSave() {
|
|
108
106
|
cancelPeriodicSave()
|
|
109
107
|
// After a natural end, setPlaybackState(ENDED) has saved position 0 and the dropped intent
|
|
110
108
|
// calls this afterwards; saving here would make resumption start at the end of the track.
|
|
111
|
-
if (
|
|
112
|
-
|
|
109
|
+
if (player.playbackState != PlaybackState.ENDED) {
|
|
110
|
+
save()
|
|
113
111
|
}
|
|
114
112
|
}
|
|
115
113
|
|
|
116
114
|
/** Saves the current track and its position; does nothing without a current track. */
|
|
117
115
|
fun save() {
|
|
118
116
|
val track = player.currentTrack ?: return
|
|
119
|
-
|
|
117
|
+
// A live item reports 0 until it has loaded, so 0 is saved unset too.
|
|
118
|
+
val positionMs =
|
|
119
|
+
player.position.takeUnless { player.isCurrentItemLive || it == 0L } ?: C.TIME_UNSET
|
|
120
120
|
|
|
121
121
|
prefs.edit {
|
|
122
122
|
putLong(KEY_POSITION_MS, positionMs)
|
|
@@ -125,13 +125,6 @@ class PlaybackStateStore(private val player: Player) {
|
|
|
125
125
|
Timber.d("Saved playback state: title=${track.title}, positionMs=$positionMs")
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
-
fun savePosition() {
|
|
129
|
-
val positionMs = if (player.isCurrentItemLive) C.TIME_UNSET else player.position
|
|
130
|
-
|
|
131
|
-
prefs.edit { putLong(KEY_POSITION_MS, positionMs) }
|
|
132
|
-
Timber.d("Saved position: positionMs=$positionMs")
|
|
133
|
-
}
|
|
134
|
-
|
|
135
128
|
fun savePositionZero() {
|
|
136
129
|
prefs.edit { putLong(KEY_POSITION_MS, 0) }
|
|
137
130
|
Timber.d("Saved position: positionMs=0 (queue ended)")
|
|
@@ -262,6 +255,6 @@ class PlaybackStateStore(private val player: Player) {
|
|
|
262
255
|
private const val KEY_SHUFFLE_ENABLED = "shuffle_enabled"
|
|
263
256
|
private const val KEY_PLAYBACK_SPEED = "playback_speed"
|
|
264
257
|
private const val KEY_TRACK = "track"
|
|
265
|
-
|
|
258
|
+
internal const val PERIODIC_SAVE_INTERVAL_MS = 5000L
|
|
266
259
|
}
|
|
267
260
|
}
|
|
@@ -4,9 +4,13 @@ import android.content.ComponentName
|
|
|
4
4
|
import android.content.Intent
|
|
5
5
|
import android.os.Binder
|
|
6
6
|
import android.os.Looper
|
|
7
|
+
import androidx.media3.common.C
|
|
7
8
|
import com.audiobrowser.model.PlayerUpdateOptions
|
|
9
|
+
import com.audiobrowser.player.PlaybackStateStore
|
|
8
10
|
import com.margelo.nitro.audiobrowser.AppKilledPlaybackBehavior
|
|
9
11
|
import com.margelo.nitro.audiobrowser.PlaybackState
|
|
12
|
+
import java.time.Duration
|
|
13
|
+
import java.util.concurrent.TimeUnit
|
|
10
14
|
import org.junit.After
|
|
11
15
|
import org.junit.Assert.assertEquals
|
|
12
16
|
import org.junit.Assert.assertFalse
|
|
@@ -70,6 +74,45 @@ class ServiceTaskRemovedTest {
|
|
|
70
74
|
assertTrue(shadowOf(service).isStoppedBySelf)
|
|
71
75
|
}
|
|
72
76
|
|
|
77
|
+
private fun playAndSavePositionAt61s() {
|
|
78
|
+
playTrack("a")
|
|
79
|
+
player.seekTo(61, TimeUnit.SECONDS)
|
|
80
|
+
shadowOf(Looper.getMainLooper())
|
|
81
|
+
.idleFor(Duration.ofMillis(PlaybackStateStore.PERIODIC_SAVE_INTERVAL_MS))
|
|
82
|
+
assertEquals(61_000L, player.playbackStateStore.get()?.positionMs)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
@Test
|
|
86
|
+
fun `a removed task keeps the saved position while AudioBrowser is bound`() {
|
|
87
|
+
controller.bind()
|
|
88
|
+
playAndSavePositionAt61s()
|
|
89
|
+
|
|
90
|
+
removeTask()
|
|
91
|
+
|
|
92
|
+
assertEquals(61_000L, player.playbackStateStore.get()?.positionMs)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
@Test
|
|
96
|
+
fun `a removed task keeps the saved position when AudioBrowser never bound`() {
|
|
97
|
+
playAndSavePositionAt61s()
|
|
98
|
+
|
|
99
|
+
removeTask()
|
|
100
|
+
|
|
101
|
+
assertEquals(61_000L, player.playbackStateStore.get()?.positionMs)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
@Test
|
|
105
|
+
fun `a stream swiped away before it has loaded resumes at its default position`() {
|
|
106
|
+
controller.bind()
|
|
107
|
+
// Unloaded, it is not yet known to be live and reports position 0, which is the window start
|
|
108
|
+
// of a live stream.
|
|
109
|
+
playTrack("a")
|
|
110
|
+
|
|
111
|
+
removeTask()
|
|
112
|
+
|
|
113
|
+
assertEquals(C.TIME_UNSET, player.playbackStateStore.get()?.positionMs)
|
|
114
|
+
}
|
|
115
|
+
|
|
73
116
|
@Test
|
|
74
117
|
fun `a removed task clears the sleep timer of the player AudioBrowser kept`() {
|
|
75
118
|
controller.bind()
|