mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 00:10:32 +05:30
Add indicators for current position and duration
This commit is contained in:
parent
2224ffc44a
commit
3890bc3bcc
@ -8,6 +8,7 @@ import android.os.Bundle
|
|||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
|
import android.text.format.DateUtils
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
@ -16,6 +17,7 @@ import com.github.libretube.api.obj.StreamItem
|
|||||||
import com.github.libretube.databinding.FragmentAudioPlayerBinding
|
import com.github.libretube.databinding.FragmentAudioPlayerBinding
|
||||||
import com.github.libretube.extensions.toID
|
import com.github.libretube.extensions.toID
|
||||||
import com.github.libretube.services.BackgroundMode
|
import com.github.libretube.services.BackgroundMode
|
||||||
|
import com.github.libretube.ui.activities.MainActivity
|
||||||
import com.github.libretube.ui.base.BaseFragment
|
import com.github.libretube.ui.base.BaseFragment
|
||||||
import com.github.libretube.ui.sheets.PlayingQueueSheet
|
import com.github.libretube.ui.sheets.PlayingQueueSheet
|
||||||
import com.github.libretube.util.ImageHelper
|
import com.github.libretube.util.ImageHelper
|
||||||
@ -31,7 +33,6 @@ class AudioPlayerFragment : BaseFragment() {
|
|||||||
private var isPaused: Boolean = false
|
private var isPaused: Boolean = false
|
||||||
|
|
||||||
private lateinit var playerService: BackgroundMode
|
private lateinit var playerService: BackgroundMode
|
||||||
private var mBound: Boolean = false
|
|
||||||
|
|
||||||
/** Defines callbacks for service binding, passed to bindService() */
|
/** Defines callbacks for service binding, passed to bindService() */
|
||||||
private val connection = object : ServiceConnection {
|
private val connection = object : ServiceConnection {
|
||||||
@ -40,11 +41,17 @@ class AudioPlayerFragment : BaseFragment() {
|
|||||||
val binder = service as BackgroundMode.LocalBinder
|
val binder = service as BackgroundMode.LocalBinder
|
||||||
playerService = binder.getService()
|
playerService = binder.getService()
|
||||||
handleServiceConnection()
|
handleServiceConnection()
|
||||||
mBound = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onServiceDisconnected(arg0: ComponentName) {
|
override fun onServiceDisconnected(arg0: ComponentName) {
|
||||||
mBound = false
|
val mainActivity = activity as MainActivity
|
||||||
|
if (mainActivity.navController.currentDestination?.id == R.id.audioPlayerFragment) {
|
||||||
|
mainActivity.navController.popBackStack()
|
||||||
|
} else {
|
||||||
|
mainActivity.navController.backQueue.removeAll {
|
||||||
|
it.destination.id == R.id.audioPlayerFragment
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,11 +76,13 @@ class AudioPlayerFragment : BaseFragment() {
|
|||||||
|
|
||||||
binding.prev.setOnClickListener {
|
binding.prev.setOnClickListener {
|
||||||
val currentIndex = PlayingQueue.currentIndex()
|
val currentIndex = PlayingQueue.currentIndex()
|
||||||
|
if (!PlayingQueue.hasPrev()) return@setOnClickListener
|
||||||
PlayingQueue.onQueueItemSelected(currentIndex - 1)
|
PlayingQueue.onQueueItemSelected(currentIndex - 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.next.setOnClickListener {
|
binding.next.setOnClickListener {
|
||||||
val currentIndex = PlayingQueue.currentIndex()
|
val currentIndex = PlayingQueue.currentIndex()
|
||||||
|
if (!PlayingQueue.hasNext()) return@setOnClickListener
|
||||||
PlayingQueue.onQueueItemSelected(currentIndex + 1)
|
PlayingQueue.onQueueItemSelected(currentIndex + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,7 +93,7 @@ class AudioPlayerFragment : BaseFragment() {
|
|||||||
PlayingQueue.addOnTrackChangedListener(onTrackChangeListener)
|
PlayingQueue.addOnTrackChangedListener(onTrackChangeListener)
|
||||||
|
|
||||||
binding.playPause.setOnClickListener {
|
binding.playPause.setOnClickListener {
|
||||||
if (!mBound) return@setOnClickListener
|
if (!this::playerService.isInitialized) return@setOnClickListener
|
||||||
if (isPaused) playerService.play() else playerService.pause()
|
if (isPaused) playerService.play() else playerService.pause()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,7 +118,10 @@ class AudioPlayerFragment : BaseFragment() {
|
|||||||
private fun initializeSeekBar() {
|
private fun initializeSeekBar() {
|
||||||
if (!this::playerService.isInitialized) return
|
if (!this::playerService.isInitialized) return
|
||||||
|
|
||||||
binding.timeBar.valueTo = (playerService.getDuration()?.toFloat() ?: return) / 1000
|
val duration = playerService.getDuration()?.toFloat() ?: return
|
||||||
|
binding.timeBar.valueTo = duration / 1000
|
||||||
|
binding.duration.text = DateUtils.formatElapsedTime((duration / 1000).toLong())
|
||||||
|
|
||||||
binding.timeBar.addOnChangeListener { _, value, fromUser ->
|
binding.timeBar.addOnChangeListener { _, value, fromUser ->
|
||||||
if (fromUser) playerService.seekToPosition(value.toLong() * 1000)
|
if (fromUser) playerService.seekToPosition(value.toLong() * 1000)
|
||||||
}
|
}
|
||||||
@ -117,10 +129,14 @@ class AudioPlayerFragment : BaseFragment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun updateCurrentPosition() {
|
private fun updateCurrentPosition() {
|
||||||
|
val currentPosition = playerService.getCurrentPosition()?.toFloat() ?: 0f
|
||||||
binding.timeBar.value = minOf(
|
binding.timeBar.value = minOf(
|
||||||
(playerService.getCurrentPosition()?.toFloat() ?: 0f) / 1000,
|
currentPosition / 1000,
|
||||||
binding.timeBar.valueTo
|
binding.timeBar.valueTo
|
||||||
)
|
)
|
||||||
|
binding.currentPosition.text = DateUtils.formatElapsedTime(
|
||||||
|
(currentPosition / 1000).toLong()
|
||||||
|
)
|
||||||
handler.postDelayed(this::updateCurrentPosition, 200)
|
handler.postDelayed(this::updateCurrentPosition, 200)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,15 +110,15 @@ import com.google.android.exoplayer2.ui.StyledPlayerView
|
|||||||
import com.google.android.exoplayer2.upstream.DefaultDataSource
|
import com.google.android.exoplayer2.upstream.DefaultDataSource
|
||||||
import com.google.android.exoplayer2.util.MimeTypes
|
import com.google.android.exoplayer2.util.MimeTypes
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
|
import java.io.IOException
|
||||||
|
import java.util.*
|
||||||
|
import java.util.concurrent.Executors
|
||||||
|
import kotlin.math.abs
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import org.chromium.net.CronetEngine
|
import org.chromium.net.CronetEngine
|
||||||
import retrofit2.HttpException
|
import retrofit2.HttpException
|
||||||
import java.io.IOException
|
|
||||||
import java.util.*
|
|
||||||
import java.util.concurrent.Executors
|
|
||||||
import kotlin.math.abs
|
|
||||||
|
|
||||||
class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
|
class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
|
||||||
|
|
||||||
|
@ -20,7 +20,6 @@ import com.github.libretube.api.obj.Streams
|
|||||||
import com.github.libretube.constants.BACKGROUND_CHANNEL_ID
|
import com.github.libretube.constants.BACKGROUND_CHANNEL_ID
|
||||||
import com.github.libretube.constants.IntentData
|
import com.github.libretube.constants.IntentData
|
||||||
import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
|
import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
|
||||||
import com.github.libretube.constants.PreferenceKeys
|
|
||||||
import com.github.libretube.ui.activities.MainActivity
|
import com.github.libretube.ui.activities.MainActivity
|
||||||
import com.google.android.exoplayer2.ExoPlayer
|
import com.google.android.exoplayer2.ExoPlayer
|
||||||
import com.google.android.exoplayer2.Player
|
import com.google.android.exoplayer2.Player
|
||||||
|
@ -59,6 +59,27 @@
|
|||||||
app:labelBehavior="gone"
|
app:labelBehavior="gone"
|
||||||
android:layout_marginHorizontal="20dp" />
|
android:layout_marginHorizontal="20dp" />
|
||||||
|
|
||||||
|
<FrameLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingHorizontal="20dp">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/current_position"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="start|center"
|
||||||
|
tools:text="00:00"/>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/duration"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="end|center"
|
||||||
|
tools:text="10:15"/>
|
||||||
|
|
||||||
|
</FrameLayout>
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user