Skip to content

Commit 9adf5e2

Browse files
committed
song elapsed time improvements
1 parent dc5ac75 commit 9adf5e2

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

Diff for: wukong/src/main/java/com/senorsen/wukong/network/message/Protocol.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ data class WebSocketReceiveProtocol(
1414

1515
val song: Song? = null,
1616
val downvote: Boolean? = null,
17-
val elapsed: Float? = null,
17+
val elapsed: Float,
1818
val user: String? = null,
1919

2020
val users: List<User>? = null,

Diff for: wukong/src/main/java/com/senorsen/wukong/service/WukongService.kt

+8-4
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ class WukongService : Service() {
533533

534534
Protocol.PRELOAD -> {
535535
val song = protocol.song!!
536+
Log.i(TAG, "debounce preload: ${song.songKey} ${song.title}")
536537
debounce.run {
537538
try {
538539
// Preload artwork image.
@@ -572,16 +573,19 @@ class WukongService : Service() {
572573

573574
currentPlayUserId = protocol.user
574575
currentPlayUser = User.getUserFromList(userList, currentPlayUserId) ?: currentPlayUser
575-
576576
downvoted = protocol.downvote ?: false
577-
val oldStartTime = songStartTime
578-
songStartTime = currentTimeMillis() - ((if (protocol.elapsed!! <= 10) 0.toFloat() else protocol.elapsed) * 1000).toLong()
579577

578+
Log.i(TAG, "Play: ${song.songKey} ${song.title} by user ${currentPlayUser?.userName}")
579+
580+
val oldStartTime = songStartTime
581+
songStartTime = currentTimeMillis() - (protocol.elapsed * 1000).toLong()
580582
// Reduce noise or glitch.
581583
if (currentSong?.songKey == song.songKey && Math.abs(songStartTime - oldStartTime) < 5000) {
582584
Log.i(TAG, "server may send exactly the same song ${song.songKey}, skipping")
583585
return
584586
}
587+
// Play from the beginning if elapsed time is too short.
588+
songStartTime = currentTimeMillis() - ((if (protocol.elapsed <= 10) 0.toFloat() else protocol.elapsed) * 1000).toLong()
585589

586590
mediaPlayer.reset()
587591
currentSong = song
@@ -639,7 +643,7 @@ class WukongService : Service() {
639643

640644
Protocol.NOTIFICATION -> {
641645
handler.post {
642-
Toast.makeText(applicationContext, "Wukong: " + protocol.notification?.message, Toast.LENGTH_SHORT).show()
646+
Toast.makeText(applicationContext, "Wukong: " + protocol.notification?.message, Toast.LENGTH_LONG).show()
643647
}
644648
}
645649
}

0 commit comments

Comments
 (0)