Move media session implementation to exoplayer module

This commit is contained in:
Niels van Velzen 2024-06-12 21:44:56 +02:00 committed by Niels van Velzen
parent d08e57c287
commit 5f3dd47243
13 changed files with 40 additions and 50 deletions

View File

@ -16,10 +16,9 @@ import org.jellyfin.androidtv.ui.playback.MediaManager
import org.jellyfin.androidtv.ui.playback.RewritePlaybackLauncher
import org.jellyfin.androidtv.ui.playback.VideoQueueManager
import org.jellyfin.androidtv.ui.playback.rewrite.RewriteMediaManager
import org.jellyfin.playback.core.mediasession.MediaSessionOptions
import org.jellyfin.playback.core.mediasession.mediaSessionPlugin
import org.jellyfin.playback.core.playbackManager
import org.jellyfin.playback.exoplayer.exoPlayerPlugin
import org.jellyfin.playback.exoplayer.session.MediaSessionOptions
import org.jellyfin.playback.jellyfin.jellyfinPlugin
import org.koin.android.ext.koin.androidContext
import org.koin.core.scope.Scope
@ -44,13 +43,10 @@ val playbackModule = module {
}
fun Scope.createPlaybackManager() = playbackManager(androidContext()) {
install(exoPlayerPlugin(get()))
install(jellyfinPlugin(get()))
val activityIntent = Intent(get(), MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(get(), 0, activityIntent, PendingIntent.FLAG_IMMUTABLE)
val notificationChannelId = "mediasession"
val notificationChannelId = "session"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
notificationChannelId,
@ -60,12 +56,13 @@ fun Scope.createPlaybackManager() = playbackManager(androidContext()) {
NotificationManagerCompat.from(get()).createNotificationChannel(channel)
}
install(mediaSessionPlugin(get(), MediaSessionOptions(
val mediaSessionOptions = MediaSessionOptions(
channelId = notificationChannelId,
notificationId = 1,
iconSmall = R.drawable.app_icon_foreground,
openIntent = pendingIntent,
)))
openIntent = pendingIntent,)
install(exoPlayerPlugin(get(), mediaSessionOptions))
install(jellyfinPlugin(get()))
// Options
val userSettingPreferences = get<UserSettingPreferences>()

View File

@ -1,7 +1,6 @@
plugins {
id("com.android.library")
kotlin("android")
alias(libs.plugins.kotlin.serialization)
}
android {
@ -29,15 +28,9 @@ android {
dependencies {
// Kotlin
implementation(libs.kotlinx.coroutines)
implementation(libs.kotlinx.coroutines.guava)
implementation(libs.kotlinx.serialization.json)
// Android(x)
implementation(libs.androidx.core)
implementation(libs.androidx.appcompat)
implementation(libs.androidx.constraintlayout)
implementation(libs.bundles.androidx.lifecycle)
implementation(libs.androidx.media3.session)
// Dependency Injection
implementation(libs.bundles.koin)

View File

@ -1,13 +1 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application>
<service
android:name="org.jellyfin.playback.core.mediasession.AndroidMediaService"
android:exported="true"
android:foregroundServiceType="mediaPlayback">
<intent-filter>
<action android:name="androidx.media3.session.MediaSessionService" />
</intent-filter>
</service>
</application>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" />

View File

@ -1,11 +0,0 @@
package org.jellyfin.playback.core.mediasession
import android.content.Context
import org.jellyfin.playback.core.plugin.playbackPlugin
fun mediaSessionPlugin(
androidContext: Context,
options: MediaSessionOptions,
) = playbackPlugin {
provide(MediaSessionService(androidContext, options))
}

View File

@ -41,7 +41,6 @@ internal class MediaStreamState(
}
}
}.launchIn(coroutineScope + Dispatchers.Main)
// TODO Register some kind of event when $current item is at -30 seconds to setNext()
}

View File

@ -32,15 +32,19 @@ dependencies {
// AndroidX
implementation(libs.androidx.core)
// ExoPlayer
// media3
implementation(libs.androidx.media3.exoplayer)
implementation(libs.androidx.media3.exoplayer.hls)
implementation(libs.jellyfin.androidx.media3.ffmpeg.decoder)
implementation(libs.androidx.media3.ui)
implementation(libs.androidx.media3.session)
// Logging
implementation(libs.timber)
// Compatibility (desugaring)
coreLibraryDesugaring(libs.android.desugar)
// Testing
testImplementation(libs.kotest.runner.junit5)
testImplementation(libs.kotest.assertions)

View File

@ -1 +1,15 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" />
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application>
<service
android:name="org.jellyfin.playback.exoplayer.session.AndroidMediaService"
android:exported="true"
android:foregroundServiceType="mediaPlayback"
tools:ignore="ExportedService">
<intent-filter>
<action android:name="androidx.media3.session.MediaSessionService" />
</intent-filter>
</service>
</application>
</manifest>

View File

@ -2,7 +2,13 @@ package org.jellyfin.playback.exoplayer
import android.content.Context
import org.jellyfin.playback.core.plugin.playbackPlugin
import org.jellyfin.playback.exoplayer.session.MediaSessionOptions
import org.jellyfin.playback.exoplayer.session.MediaSessionService
fun exoPlayerPlugin(androidContext: Context) = playbackPlugin {
fun exoPlayerPlugin(
androidContext: Context,
mediaSessionOptions: MediaSessionOptions,
) = playbackPlugin {
provide(ExoPlayerBackend(androidContext))
provide(MediaSessionService(androidContext, mediaSessionOptions))
}

View File

@ -1,4 +1,4 @@
package org.jellyfin.playback.core.mediasession
package org.jellyfin.playback.exoplayer.session
import androidx.media3.session.MediaSession
import androidx.media3.session.MediaSessionService

View File

@ -1,4 +1,4 @@
package org.jellyfin.playback.core.mediasession
package org.jellyfin.playback.exoplayer.session
import android.app.PendingIntent
import androidx.annotation.DrawableRes

View File

@ -1,4 +1,4 @@
package org.jellyfin.playback.core.mediasession
package org.jellyfin.playback.exoplayer.session
import android.os.Looper
import androidx.annotation.OptIn

View File

@ -1,4 +1,4 @@
package org.jellyfin.playback.core.mediasession
package org.jellyfin.playback.exoplayer.session
import android.content.Context
import android.os.Looper

View File

@ -1,4 +1,4 @@
package org.jellyfin.playback.core.mediasession
package org.jellyfin.playback.exoplayer.session
import androidx.core.net.toUri
import androidx.media3.common.MediaItem
@ -6,7 +6,7 @@ import androidx.media3.common.MediaMetadata
import org.jellyfin.playback.core.queue.QueueEntryMetadata
fun QueueEntryMetadata.toMediaItem() = MediaItem.Builder().apply {
if (mediaId != null) setMediaId(mediaId)
mediaId?.let { setMediaId(it) }
setMediaMetadata(MediaMetadata.Builder().apply {
setTitle(title)