mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 22:30:30 +05:30
Switch to LifecycleService in the offline audio player
This commit is contained in:
parent
dcdd1af176
commit
185ad31bf0
@ -1,9 +1,11 @@
|
|||||||
package com.github.libretube.services
|
package com.github.libretube.services
|
||||||
|
|
||||||
import android.app.Service
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.os.IBinder
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
import androidx.core.app.ServiceCompat
|
import androidx.core.app.ServiceCompat
|
||||||
|
import androidx.lifecycle.LifecycleService
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
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
|
||||||
@ -18,7 +20,6 @@ import com.github.libretube.obj.PlayerNotificationData
|
|||||||
import com.github.libretube.util.NowPlayingNotification
|
import com.github.libretube.util.NowPlayingNotification
|
||||||
import com.google.android.exoplayer2.ExoPlayer
|
import com.google.android.exoplayer2.ExoPlayer
|
||||||
import com.google.android.exoplayer2.MediaItem
|
import com.google.android.exoplayer2.MediaItem
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
@ -26,7 +27,7 @@ import kotlinx.coroutines.withContext
|
|||||||
/**
|
/**
|
||||||
* A service to play downloaded audio in the background
|
* A service to play downloaded audio in the background
|
||||||
*/
|
*/
|
||||||
class OfflinePlayerService : Service() {
|
class OfflinePlayerService : LifecycleService() {
|
||||||
private var player: ExoPlayer? = null
|
private var player: ExoPlayer? = null
|
||||||
private var nowPlayingNotification: NowPlayingNotification? = null
|
private var nowPlayingNotification: NowPlayingNotification? = null
|
||||||
|
|
||||||
@ -42,10 +43,10 @@ class OfflinePlayerService : Service() {
|
|||||||
startForeground(PLAYER_NOTIFICATION_ID, notification)
|
startForeground(PLAYER_NOTIFICATION_ID, notification)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
val videoId = intent.getStringExtra(IntentData.videoId)
|
val videoId = intent?.getStringExtra(IntentData.videoId)
|
||||||
|
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
lifecycleScope.launch(Dispatchers.IO) {
|
||||||
val downloadWithItems = DatabaseHolder.Database.downloadDao().findById(videoId!!)
|
val downloadWithItems = DatabaseHolder.Database.downloadDao().findById(videoId!!)
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
if (startAudioPlayer(downloadWithItems)) {
|
if (startAudioPlayer(downloadWithItems)) {
|
||||||
@ -110,5 +111,8 @@ class OfflinePlayerService : Service() {
|
|||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBind(intent: Intent?) = null
|
override fun onBind(intent: Intent): IBinder? {
|
||||||
|
super.onBind(intent)
|
||||||
|
return null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user