Skip to content

Commit

Permalink
Fix background playback stopping on Android 14 (#1412)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
michd committed Jun 25, 2024
1 parent 1665203 commit ba2d6be
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
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,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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ba2d6be

Please sign in to comment.