mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 06:10:31 +05:30
React to changes in the player notification
This commit is contained in:
parent
186925dfa1
commit
b09b9ad3bf
@ -93,6 +93,11 @@ class BackgroundMode : Service() {
|
||||
*/
|
||||
private val binder = LocalBinder()
|
||||
|
||||
/**
|
||||
* Listener for passing playback state changes to the AudioPlayerFragment
|
||||
*/
|
||||
var onIsPlayingChanged: ((isPlaying: Boolean) -> Unit)? = null
|
||||
|
||||
/**
|
||||
* Setting the required [Notification] for running as a foreground service
|
||||
*/
|
||||
@ -257,6 +262,11 @@ class BackgroundMode : Service() {
|
||||
* Plays the next video when the current one ended
|
||||
*/
|
||||
player?.addListener(object : Player.Listener {
|
||||
override fun onIsPlayingChanged(isPlaying: Boolean) {
|
||||
super.onIsPlayingChanged(isPlaying)
|
||||
onIsPlayingChanged?.invoke(isPlaying)
|
||||
}
|
||||
|
||||
override fun onPlaybackStateChanged(state: Int) {
|
||||
when (state) {
|
||||
Player.STATE_ENDED -> {
|
||||
@ -398,6 +408,8 @@ class BackgroundMode : Service() {
|
||||
|
||||
fun getCurrentPosition() = player?.currentPosition
|
||||
|
||||
fun getDuration() = player?.duration
|
||||
|
||||
fun seekToPosition(position: Long) = player?.seekTo(position)
|
||||
|
||||
fun pause() {
|
||||
|
@ -35,6 +35,7 @@ class AudioPlayerFragment : BaseFragment() {
|
||||
// We've bound to LocalService, cast the IBinder and get LocalService instance
|
||||
val binder = service as BackgroundMode.LocalBinder
|
||||
playerService = binder.getService()
|
||||
handleServiceConnection()
|
||||
mBound = true
|
||||
}
|
||||
|
||||
@ -97,9 +98,19 @@ class AudioPlayerFragment : BaseFragment() {
|
||||
ImageHelper.loadImage(current.thumbnail, binding.thumbnail)
|
||||
}
|
||||
|
||||
private fun handleServiceConnection() {
|
||||
playerService.onIsPlayingChanged = { isPlaying ->
|
||||
binding.playPause.setIconResource(
|
||||
if (isPlaying) R.drawable.ic_pause else R.drawable.ic_play
|
||||
)
|
||||
isPaused = !isPlaying
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
|
||||
playerService.onIsPlayingChanged = null
|
||||
activity?.unbindService(connection)
|
||||
// unregister the listener
|
||||
PlayingQueue.removeOnTrackChangedListener(onTrackChangeListener)
|
||||
|
Loading…
Reference in New Issue
Block a user