Fix background playback stopping on Android 14 (#1412)

* Fix background playback stopping on android 14

The service was not being properly started as a foreground service for
media playback, causing playback stoppage after every track (or sooner
for some folks), on android 14.

Different vendors may be more aggressive with killing of non-foreground
service etc, so people may have gotten different results.

* Remove redundant fg service behavior, appease lint
This commit is contained in:
MichD 2024-06-25 22:51:22 +01:00 committed by GitHub
parent 166520363e
commit ba2d6bede1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 3 deletions

View File

@ -67,7 +67,12 @@
</intent-filter>
</activity>
<service android:name=".webapp.RemotePlayerService" />
<service
android:name=".webapp.RemotePlayerService"
android:exported="false"
android:foregroundServiceType="mediaPlayback">
</service>
<service
android:name="org.jellyfin.mobile.player.audio.MediaService"

View File

@ -6,6 +6,7 @@ import android.content.Intent
import android.media.session.PlaybackState
import android.net.Uri
import android.webkit.JavascriptInterface
import androidx.core.content.ContextCompat
import org.jellyfin.mobile.events.ActivityEvent
import org.jellyfin.mobile.events.ActivityEventHandler
import org.jellyfin.mobile.utils.Constants
@ -98,7 +99,8 @@ class NativeInterface(private val context: Context) : KoinComponent {
putExtra(EXTRA_IS_LOCAL_PLAYER, options.optBoolean(EXTRA_IS_LOCAL_PLAYER, true))
putExtra(EXTRA_IS_PAUSED, options.optBoolean(EXTRA_IS_PAUSED, true))
}
context.startService(intent)
ContextCompat.startForegroundService(context, intent)
// We may need to request bluetooth permission to react to bluetooth disconnect events
activityEventHandler.emit(ActivityEvent.RequestBluetoothPermission)

View File

@ -11,6 +11,7 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.ServiceInfo
import android.graphics.Bitmap
import android.media.AudioAttributes
import android.media.AudioManager
@ -263,6 +264,7 @@ class RemotePlayerService : Service(), CoroutineScope {
} else {
setPriority(Notification.PRIORITY_LOW)
}
setContentTitle(title?.let { HtmlCompat.fromHtml(it, HtmlCompat.FROM_HTML_MODE_LEGACY) })
setContentText(artist?.let { HtmlCompat.fromHtml(it, HtmlCompat.FROM_HTML_MODE_LEGACY) })
setSubText(album)
@ -340,7 +342,18 @@ class RemotePlayerService : Service(), CoroutineScope {
}.build()
// Post notification
notificationManager.notify(MEDIA_PLAYER_NOTIFICATION_ID, notification)
if (AndroidVersion.isAtLeastQ) {
startForeground(
MEDIA_PLAYER_NOTIFICATION_ID,
notification,
ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST,
)
} else {
startForeground(
MEDIA_PLAYER_NOTIFICATION_ID,
notification,
)
}
// Activate MediaSession
mediaSession.isActive = true