mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 16:00:31 +05:30
Add indicators for current position and duration
This commit is contained in:
parent
2224ffc44a
commit
3890bc3bcc
@ -334,8 +334,8 @@ class MainActivity : BaseActivity() {
|
||||
}
|
||||
// Handover back press to `BackPressedDispatcher`
|
||||
else if (binding.bottomNav.menu.children.none {
|
||||
it.itemId == navController.currentDestination?.id
|
||||
}
|
||||
it.itemId == navController.currentDestination?.id
|
||||
}
|
||||
) {
|
||||
this@MainActivity.onBackPressedDispatcher.onBackPressed()
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.IBinder
|
||||
import android.os.Looper
|
||||
import android.text.format.DateUtils
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
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.extensions.toID
|
||||
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.sheets.PlayingQueueSheet
|
||||
import com.github.libretube.util.ImageHelper
|
||||
@ -31,7 +33,6 @@ class AudioPlayerFragment : BaseFragment() {
|
||||
private var isPaused: Boolean = false
|
||||
|
||||
private lateinit var playerService: BackgroundMode
|
||||
private var mBound: Boolean = false
|
||||
|
||||
/** Defines callbacks for service binding, passed to bindService() */
|
||||
private val connection = object : ServiceConnection {
|
||||
@ -40,11 +41,17 @@ class AudioPlayerFragment : BaseFragment() {
|
||||
val binder = service as BackgroundMode.LocalBinder
|
||||
playerService = binder.getService()
|
||||
handleServiceConnection()
|
||||
mBound = true
|
||||
}
|
||||
|
||||
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 {
|
||||
val currentIndex = PlayingQueue.currentIndex()
|
||||
if (!PlayingQueue.hasPrev()) return@setOnClickListener
|
||||
PlayingQueue.onQueueItemSelected(currentIndex - 1)
|
||||
}
|
||||
|
||||
binding.next.setOnClickListener {
|
||||
val currentIndex = PlayingQueue.currentIndex()
|
||||
if (!PlayingQueue.hasNext()) return@setOnClickListener
|
||||
PlayingQueue.onQueueItemSelected(currentIndex + 1)
|
||||
}
|
||||
|
||||
@ -84,7 +93,7 @@ class AudioPlayerFragment : BaseFragment() {
|
||||
PlayingQueue.addOnTrackChangedListener(onTrackChangeListener)
|
||||
|
||||
binding.playPause.setOnClickListener {
|
||||
if (!mBound) return@setOnClickListener
|
||||
if (!this::playerService.isInitialized) return@setOnClickListener
|
||||
if (isPaused) playerService.play() else playerService.pause()
|
||||
}
|
||||
|
||||
@ -109,7 +118,10 @@ class AudioPlayerFragment : BaseFragment() {
|
||||
private fun initializeSeekBar() {
|
||||
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 ->
|
||||
if (fromUser) playerService.seekToPosition(value.toLong() * 1000)
|
||||
}
|
||||
@ -117,10 +129,14 @@ class AudioPlayerFragment : BaseFragment() {
|
||||
}
|
||||
|
||||
private fun updateCurrentPosition() {
|
||||
val currentPosition = playerService.getCurrentPosition()?.toFloat() ?: 0f
|
||||
binding.timeBar.value = minOf(
|
||||
(playerService.getCurrentPosition()?.toFloat() ?: 0f) / 1000,
|
||||
currentPosition / 1000,
|
||||
binding.timeBar.valueTo
|
||||
)
|
||||
binding.currentPosition.text = DateUtils.formatElapsedTime(
|
||||
(currentPosition / 1000).toLong()
|
||||
)
|
||||
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.util.MimeTypes
|
||||
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.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import org.chromium.net.CronetEngine
|
||||
import retrofit2.HttpException
|
||||
import java.io.IOException
|
||||
import java.util.*
|
||||
import java.util.concurrent.Executors
|
||||
import kotlin.math.abs
|
||||
|
||||
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.IntentData
|
||||
import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
|
||||
import com.github.libretube.constants.PreferenceKeys
|
||||
import com.github.libretube.ui.activities.MainActivity
|
||||
import com.google.android.exoplayer2.ExoPlayer
|
||||
import com.google.android.exoplayer2.Player
|
||||
|
@ -59,6 +59,27 @@
|
||||
app:labelBehavior="gone"
|
||||
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
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
|
Loading…
x
Reference in New Issue
Block a user