fix: background service audio playback when screen locked (#5142)

This commit is contained in:
Paul Scheduikat 2023-11-15 16:55:09 +01:00 committed by GitHub
parent fca9aed07d
commit 981890acb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 0 deletions

View File

@ -14,7 +14,10 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:name=".LibreTubeApp"
@ -350,16 +353,19 @@
<service
android:name=".services.DownloadService"
android:foregroundServiceType="dataSync"
android:enabled="true"
android:exported="false" />
<service
android:name=".services.OnlinePlayerService"
android:foregroundServiceType="mediaPlayback"
android:enabled="true"
android:exported="false" />
<service
android:name=".services.OfflinePlayerService"
android:foregroundServiceType="mediaPlayback"
android:enabled="true"
android:exported="false" />

View File

@ -86,6 +86,8 @@ class OfflinePlayerService : LifecycleService() {
}
player = PlayerHelper.createPlayer(this, trackSelector, true)
// prevent android from putting LibreTube to sleep when locked
player!!.setWakeMode(C.WAKE_MODE_LOCAL)
val audioItem = downloadWithItem.downloadItems.filter { it.path.exists() }
.firstOrNull { it.type == FileType.AUDIO }

View File

@ -13,6 +13,7 @@ import androidx.core.net.toUri
import androidx.lifecycle.LifecycleService
import androidx.lifecycle.lifecycleScope
import androidx.media3.common.C
import androidx.media3.common.C.WAKE_MODE_NETWORK
import androidx.media3.common.MediaItem
import androidx.media3.common.MimeTypes
import androidx.media3.common.PlaybackException
@ -247,6 +248,8 @@ class OnlinePlayerService : LifecycleService() {
}
player = PlayerHelper.createPlayer(this, trackSelector, true)
// prevent android from putting LibreTube to sleep when locked
player!!.setWakeMode(WAKE_MODE_NETWORK)
/**
* Listens for changed playbackStates (e.g. pause, end)