@@ -13,6 +13,8 @@ import com.doublesymmetry.kotlinaudio.models.*
13
13
import com.doublesymmetry.kotlinaudio.models.NotificationButton.*
14
14
import com.doublesymmetry.kotlinaudio.players.QueuedAudioPlayer
15
15
import com.doublesymmetry.trackplayer.R
16
+ import com.doublesymmetry.trackplayer.extensions.NumberExt.Companion.toMilliseconds
17
+ import com.doublesymmetry.trackplayer.extensions.NumberExt.Companion.toSeconds
16
18
import com.doublesymmetry.trackplayer.extensions.asLibState
17
19
import com.doublesymmetry.trackplayer.model.Track
18
20
import com.doublesymmetry.trackplayer.model.TrackAudioItem
@@ -65,10 +67,10 @@ class MusicService : HeadlessJsTaskService() {
65
67
fun setupPlayer (playerOptions : Bundle ? ) {
66
68
// fun setupPlayer(playerOptions: Bundle?) {
67
69
val bufferOptions = BufferConfig (
68
- playerOptions?.getDouble(MIN_BUFFER_KEY )?.let { Utils .toMillis(it) .toInt() } ,
69
- playerOptions?.getDouble(MAX_BUFFER_KEY )?.let { Utils .toMillis(it) .toInt() } ,
70
- playerOptions?.getDouble(PLAY_BUFFER_KEY )?.let { Utils .toMillis(it) .toInt() } ,
71
- playerOptions?.getDouble(BACK_BUFFER_KEY )?.let { Utils .toMillis(it) .toInt() } ,
70
+ playerOptions?.getDouble(MIN_BUFFER_KEY )?.toMilliseconds()? .toInt(),
71
+ playerOptions?.getDouble(MAX_BUFFER_KEY )?.toMilliseconds()? .toInt(),
72
+ playerOptions?.getDouble(PLAY_BUFFER_KEY )?.toMilliseconds()? .toInt(),
73
+ playerOptions?.getDouble(BACK_BUFFER_KEY )?.toMilliseconds()? .toInt(),
72
74
)
73
75
74
76
val cacheOptions = CacheConfig (
@@ -170,9 +172,9 @@ class MusicService : HeadlessJsTaskService() {
170
172
private suspend fun progressUpdateEvent (): Bundle {
171
173
return withContext(Dispatchers .Main ) {
172
174
Bundle ().apply {
173
- putDouble(POSITION_KEY , player.position.toDouble() / 1000 )
174
- putDouble(DURATION_KEY , player.duration.toDouble() / 1000 )
175
- putDouble(BUFFERED_POSITION_KEY , player.bufferedPosition.toDouble() / 1000 )
175
+ putDouble(POSITION_KEY , player.position.toSeconds() )
176
+ putDouble(DURATION_KEY , player.duration.toSeconds() )
177
+ putDouble(BUFFERED_POSITION_KEY , player.bufferedPosition.toSeconds() )
176
178
putInt(TRACK_KEY , player.currentIndex)
177
179
}
178
180
}
@@ -260,7 +262,7 @@ class MusicService : HeadlessJsTaskService() {
260
262
261
263
@MainThread
262
264
fun seekTo (seconds : Float ) {
263
- player.seek((seconds * 1000 ).toLong( ), TimeUnit .MILLISECONDS )
265
+ player.seek((seconds.toMilliseconds() ), TimeUnit .MILLISECONDS )
264
266
}
265
267
266
268
@MainThread
@@ -291,13 +293,13 @@ class MusicService : HeadlessJsTaskService() {
291
293
}
292
294
293
295
@MainThread
294
- fun getDurationInSeconds (): Double = player.duration.toDouble() / 1000
296
+ fun getDurationInSeconds (): Double = player.duration.toSeconds()
295
297
296
298
@MainThread
297
- fun getPositionInSeconds (): Double = player.position.toDouble() / 1000
299
+ fun getPositionInSeconds (): Double = player.position.toSeconds()
298
300
299
301
@MainThread
300
- fun getBufferedPositionInSeconds (): Double = player.bufferedPosition.toDouble() / 1000
302
+ fun getBufferedPositionInSeconds (): Double = player.bufferedPosition.toSeconds()
301
303
302
304
@MainThread
303
305
fun updateMetadataForTrack (index : Int , track : Track ) {
@@ -323,28 +325,29 @@ class MusicService : HeadlessJsTaskService() {
323
325
emit(MusicEvents .PLAYBACK_STATE , bundle)
324
326
325
327
if (it == AudioPlayerState .ENDED && player.nextItem == null ) {
326
- val endBundle = Bundle ()
327
- endBundle. putInt(TRACK_KEY , player.currentIndex)
328
- endBundle. putDouble(POSITION_KEY , Utils .toSeconds( player.position))
328
+ Bundle (). apply {
329
+ putInt(TRACK_KEY , player.currentIndex)
330
+ putDouble(POSITION_KEY , player.position.toSeconds( ))
329
331
330
- emit(MusicEvents .PLAYBACK_QUEUE_ENDED , endBundle)
331
- emit(MusicEvents .PLAYBACK_TRACK_CHANGED , endBundle)
332
+ emit(MusicEvents .PLAYBACK_QUEUE_ENDED , this )
333
+ emit(MusicEvents .PLAYBACK_TRACK_CHANGED , this )
334
+ }
332
335
}
333
336
}
334
337
}
335
338
336
339
scope.launch {
337
340
event.audioItemTransition.collect {
338
341
Bundle ().apply {
339
- putDouble(POSITION_KEY , 0.0 )
342
+ putDouble(POSITION_KEY , (it?.oldPosition ? : 0 ).toSeconds() )
340
343
putInt(NEXT_TRACK_KEY , player.currentIndex)
341
344
342
345
// correctly set the previous index on the event payload
343
346
var previousIndex: Int? = null
344
- if (it == AudioItemTransitionReason .REPEAT ) {
347
+ if (it is AudioItemTransitionReason .REPEAT ) {
345
348
previousIndex = player.currentIndex
346
349
} else if (player.previousItem != null ) {
347
- previousIndex = player? .previousIndex
350
+ previousIndex = player.previousIndex
348
351
}
349
352
350
353
if (previousIndex != null ) {
@@ -377,8 +380,8 @@ class MusicService : HeadlessJsTaskService() {
377
380
is FORWARD -> {
378
381
Bundle ().apply {
379
382
val interval = latestOptions?.getDouble(
380
- FORWARD_JUMP_INTERVAL_KEY ,
381
- DEFAULT_JUMP_INTERVAL ,
383
+ FORWARD_JUMP_INTERVAL_KEY ,
384
+ DEFAULT_JUMP_INTERVAL ,
382
385
) ? : DEFAULT_JUMP_INTERVAL
383
386
putInt(" interval" , interval.toInt())
384
387
emit(MusicEvents .BUTTON_JUMP_FORWARD , this )
@@ -387,8 +390,8 @@ class MusicService : HeadlessJsTaskService() {
387
390
is BACKWARD -> {
388
391
Bundle ().apply {
389
392
val interval = latestOptions?.getDouble(
390
- BACKWARD_JUMP_INTERVAL_KEY ,
391
- DEFAULT_JUMP_INTERVAL ,
393
+ BACKWARD_JUMP_INTERVAL_KEY ,
394
+ DEFAULT_JUMP_INTERVAL ,
392
395
) ? : DEFAULT_JUMP_INTERVAL
393
396
putInt(" interval" , interval.toInt())
394
397
emit(MusicEvents .BUTTON_JUMP_BACKWARD , this )
0 commit comments