mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 22:30:30 +05:30
Add play and pause functionality
This commit is contained in:
parent
59697caebf
commit
186925dfa1
@ -5,6 +5,7 @@ import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.Service
|
||||
import android.content.Intent
|
||||
import android.os.Binder
|
||||
import android.os.Build
|
||||
import android.os.Handler
|
||||
import android.os.IBinder
|
||||
@ -87,6 +88,11 @@ class BackgroundMode : Service() {
|
||||
*/
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
|
||||
/**
|
||||
* Used for connecting to the AudioPlayerFragment
|
||||
*/
|
||||
private val binder = LocalBinder()
|
||||
|
||||
/**
|
||||
* Setting the required [Notification] for running as a foreground service
|
||||
*/
|
||||
@ -381,7 +387,24 @@ class BackgroundMode : Service() {
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
override fun onBind(p0: Intent?): IBinder? {
|
||||
return null
|
||||
inner class LocalBinder : Binder() {
|
||||
// Return this instance of [BackgroundMode] so clients can call public methods
|
||||
fun getService(): BackgroundMode = this@BackgroundMode
|
||||
}
|
||||
|
||||
override fun onBind(p0: Intent?): IBinder {
|
||||
return binder
|
||||
}
|
||||
|
||||
fun getCurrentPosition() = player?.currentPosition
|
||||
|
||||
fun seekToPosition(position: Long) = player?.seekTo(position)
|
||||
|
||||
fun pause() {
|
||||
player?.pause()
|
||||
}
|
||||
|
||||
fun play() {
|
||||
player?.play()
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,19 @@
|
||||
package com.github.libretube.ui.fragments
|
||||
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.ServiceConnection
|
||||
import android.os.Bundle
|
||||
import android.os.IBinder
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import com.github.libretube.R
|
||||
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.base.BaseFragment
|
||||
import com.github.libretube.util.ImageHelper
|
||||
import com.github.libretube.util.NavigationHelper
|
||||
@ -17,6 +24,31 @@ class AudioPlayerFragment : BaseFragment() {
|
||||
private val onTrackChangeListener: (StreamItem) -> Unit = {
|
||||
updateStreamInfo()
|
||||
}
|
||||
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 {
|
||||
override fun onServiceConnected(className: ComponentName, service: IBinder) {
|
||||
// We've bound to LocalService, cast the IBinder and get LocalService instance
|
||||
val binder = service as BackgroundMode.LocalBinder
|
||||
playerService = binder.getService()
|
||||
mBound = true
|
||||
}
|
||||
|
||||
override fun onServiceDisconnected(arg0: ComponentName) {
|
||||
mBound = false
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
Intent(activity, BackgroundMode::class.java).also { intent ->
|
||||
activity?.bindService(intent, connection, Context.BIND_AUTO_CREATE)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
@ -42,6 +74,13 @@ class AudioPlayerFragment : BaseFragment() {
|
||||
|
||||
PlayingQueue.addOnTrackChangedListener(onTrackChangeListener)
|
||||
|
||||
binding.playPause.setOnClickListener {
|
||||
if (mBound == false) return@setOnClickListener
|
||||
if (isPaused) playerService.play() else playerService.pause()
|
||||
binding.playPause.setIconResource(if (isPaused) R.drawable.ic_pause else R.drawable.ic_play)
|
||||
isPaused = !isPaused
|
||||
}
|
||||
|
||||
updateStreamInfo()
|
||||
}
|
||||
|
||||
@ -61,6 +100,7 @@ class AudioPlayerFragment : BaseFragment() {
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
|
||||
activity?.unbindService(connection)
|
||||
// unregister the listener
|
||||
PlayingQueue.removeOnTrackChangedListener(onTrackChangeListener)
|
||||
}
|
||||
|
@ -68,8 +68,10 @@ object PlayingQueue {
|
||||
fun updateCurrent(streamItem: StreamItem) {
|
||||
currentStream = streamItem
|
||||
onTrackChangedListeners.forEach {
|
||||
runCatching {
|
||||
it.invoke(streamItem)
|
||||
}
|
||||
}
|
||||
if (!contains(streamItem)) queue.add(streamItem)
|
||||
}
|
||||
|
||||
|
@ -81,7 +81,7 @@
|
||||
android:insetTop="0dp"
|
||||
android:insetRight="0dp"
|
||||
android:insetBottom="0dp"
|
||||
app:icon="@drawable/ic_play"
|
||||
app:icon="@drawable/ic_pause"
|
||||
app:iconSize="24dp"
|
||||
app:shapeAppearanceOverlay="@style/ShapeAppearance.Material3.Corner.Full" />
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user