Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix background playback stopping on Android 14 #1412

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -263,6 +264,11 @@ class RemotePlayerService : Service(), CoroutineScope {
} else {
setPriority(Notification.PRIORITY_LOW)
}

if (AndroidVersion.isAtLeastS) {
setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE)
}
Maxr1998 marked this conversation as resolved.
Show resolved Hide resolved

setContentTitle(title?.let { HtmlCompat.fromHtml(it, HtmlCompat.FROM_HTML_MODE_LEGACY) })
setContentText(artist?.let { HtmlCompat.fromHtml(it, HtmlCompat.FROM_HTML_MODE_LEGACY) })
setSubText(album)
Expand Down Expand Up @@ -340,7 +346,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
)
}
Maxr1998 marked this conversation as resolved.
Show resolved Hide resolved

// Activate MediaSession
mediaSession.isActive = true
Expand Down
Loading