Make the seekbar functional

This commit is contained in:
Bnyro 2023-01-13 18:53:57 +01:00
parent b09b9ad3bf
commit 4a5bed6f99
2 changed files with 19 additions and 3 deletions

View File

@ -5,7 +5,9 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.ServiceConnection import android.content.ServiceConnection
import android.os.Bundle import android.os.Bundle
import android.os.Handler
import android.os.IBinder import android.os.IBinder
import android.os.Looper
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -24,6 +26,7 @@ class AudioPlayerFragment : BaseFragment() {
private val onTrackChangeListener: (StreamItem) -> Unit = { private val onTrackChangeListener: (StreamItem) -> Unit = {
updateStreamInfo() updateStreamInfo()
} }
private var handler = Handler(Looper.getMainLooper())
private var isPaused: Boolean = false private var isPaused: Boolean = false
private lateinit var playerService: BackgroundMode private lateinit var playerService: BackgroundMode
@ -78,8 +81,6 @@ class AudioPlayerFragment : BaseFragment() {
binding.playPause.setOnClickListener { binding.playPause.setOnClickListener {
if (mBound == false) return@setOnClickListener if (mBound == false) return@setOnClickListener
if (isPaused) playerService.play() else playerService.pause() if (isPaused) playerService.play() else playerService.pause()
binding.playPause.setIconResource(if (isPaused) R.drawable.ic_pause else R.drawable.ic_play)
isPaused = !isPaused
} }
updateStreamInfo() updateStreamInfo()
@ -98,6 +99,19 @@ class AudioPlayerFragment : BaseFragment() {
ImageHelper.loadImage(current.thumbnail, binding.thumbnail) ImageHelper.loadImage(current.thumbnail, binding.thumbnail)
} }
private fun initializeSeekBar() {
binding.timeBar.valueTo = playerService.getDuration()?.toFloat() ?: return
binding.timeBar.addOnChangeListener { _, value, fromUser ->
if (fromUser) playerService.seekToPosition(value.toLong())
}
updateCurrentPosition()
}
private fun updateCurrentPosition() {
binding.timeBar.value = playerService.getCurrentPosition()?.toFloat() ?: 0f
handler.postDelayed(this::updateCurrentPosition, 200)
}
private fun handleServiceConnection() { private fun handleServiceConnection() {
playerService.onIsPlayingChanged = { isPlaying -> playerService.onIsPlayingChanged = { isPlaying ->
binding.playPause.setIconResource( binding.playPause.setIconResource(
@ -105,6 +119,7 @@ class AudioPlayerFragment : BaseFragment() {
) )
isPaused = !isPlaying isPaused = !isPlaying
} }
initializeSeekBar()
} }
override fun onDestroy() { override fun onDestroy() {

View File

@ -40,13 +40,14 @@
android:marqueeRepeatLimit="marquee_forever" android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true" android:scrollHorizontally="true"
android:singleLine="true" android:singleLine="true"
android:textSize="20sp" /> android:textSize="24sp" />
<TextView <TextView
android:id="@+id/uploader" android:id="@+id/uploader"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_gravity="center"
android:textSize="18sp"
android:layout_marginTop="10dp" /> android:layout_marginTop="10dp" />
</LinearLayout> </LinearLayout>