Skip to content

Commit

Permalink
Update media3 and fix some issues with MediaService (readium#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
qnga authored Feb 28, 2024
1 parent 8d74923 commit 5746e7f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ androidx-lifecycle = "2.7.0"
androidx-lifecycle-extensions = "2.2.0"
androidx-media = "1.7.0"
androidx-media2 = "1.3.0"
androidx-media3 = "1.2.1"
androidx-media3 = "1.3.0-rc01"
androidx-navigation = "2.7.6"
androidx-paging = "3.2.1"
androidx-recyclerview = "1.3.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import android.content.Intent
import android.content.ServiceConnection
import android.os.Build
import android.os.IBinder
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.ServiceCompat
import androidx.media3.session.DefaultMediaNotificationProvider
import androidx.media3.session.MediaSession
import androidx.media3.session.MediaSessionService
import kotlinx.coroutines.*
Expand Down Expand Up @@ -137,6 +139,34 @@ class MediaService : MediaSessionService() {
}
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
val readerRepository = (application as org.readium.r2.testapp.Application).readerRepository

// App and service can be started again from a stale notification using
// PendingIntent.getForegroundService, so we need to call startForeground and then stop
// the service.
if (readerRepository.isEmpty()) {
val notification =
NotificationCompat.Builder(
this,
DefaultMediaNotificationProvider.DEFAULT_CHANNEL_ID
)
.setContentTitle("Media service")
.setContentText("Media service will stop immediately.")
.build()

// Unfortunately, stopSelf does not remove the need for calling startForeground
// to prevent crashing.
startForeground(DefaultMediaNotificationProvider.DEFAULT_NOTIFICATION_ID, notification)
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
stopSelf(startId)
}

// Prevents the service from being automatically restarted after being killed;
return START_NOT_STICKY
}

override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaSession? {
return binder.session.value?.mediaSession
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class ReaderRepository(
private val mediaServiceFacade: MediaServiceFacade =
MediaServiceFacade(application)

fun isEmpty() =
repository.isEmpty()

operator fun get(bookId: Long): ReaderInitData? =
repository[bookId]

Expand Down Expand Up @@ -124,7 +127,13 @@ class ReaderRepository(
)
}

return readerInitData.map { repository[bookId] = it }
return readerInitData.map {
repository[bookId] = it

if (it is MediaReaderInitData) {
mediaServiceFacade.openSession(bookId, it.mediaNavigator)
}
}
}

private suspend fun openAudio(
Expand Down Expand Up @@ -159,7 +168,6 @@ class ReaderRepository(
)
}

mediaServiceFacade.openSession(bookId, navigator)
val initData = MediaReaderInitData(
bookId,
publication,
Expand Down

0 comments on commit 5746e7f

Please sign in to comment.