Skip to content

Commit 5746e7f

Browse files
authored
Update media3 and fix some issues with MediaService (#478)
1 parent 8d74923 commit 5746e7f

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

gradle/libs.versions.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ androidx-lifecycle = "2.7.0"
3030
androidx-lifecycle-extensions = "2.2.0"
3131
androidx-media = "1.7.0"
3232
androidx-media2 = "1.3.0"
33-
androidx-media3 = "1.2.1"
33+
androidx-media3 = "1.3.0-rc01"
3434
androidx-navigation = "2.7.6"
3535
androidx-paging = "3.2.1"
3636
androidx-recyclerview = "1.3.2"

test-app/src/main/java/org/readium/r2/testapp/reader/MediaService.kt

+30
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import android.content.Intent
1313
import android.content.ServiceConnection
1414
import android.os.Build
1515
import android.os.IBinder
16+
import androidx.core.app.NotificationCompat
1617
import androidx.core.app.NotificationManagerCompat
1718
import androidx.core.app.ServiceCompat
19+
import androidx.media3.session.DefaultMediaNotificationProvider
1820
import androidx.media3.session.MediaSession
1921
import androidx.media3.session.MediaSessionService
2022
import kotlinx.coroutines.*
@@ -137,6 +139,34 @@ class MediaService : MediaSessionService() {
137139
}
138140
}
139141

142+
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
143+
super.onStartCommand(intent, flags, startId)
144+
val readerRepository = (application as org.readium.r2.testapp.Application).readerRepository
145+
146+
// App and service can be started again from a stale notification using
147+
// PendingIntent.getForegroundService, so we need to call startForeground and then stop
148+
// the service.
149+
if (readerRepository.isEmpty()) {
150+
val notification =
151+
NotificationCompat.Builder(
152+
this,
153+
DefaultMediaNotificationProvider.DEFAULT_CHANNEL_ID
154+
)
155+
.setContentTitle("Media service")
156+
.setContentText("Media service will stop immediately.")
157+
.build()
158+
159+
// Unfortunately, stopSelf does not remove the need for calling startForeground
160+
// to prevent crashing.
161+
startForeground(DefaultMediaNotificationProvider.DEFAULT_NOTIFICATION_ID, notification)
162+
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
163+
stopSelf(startId)
164+
}
165+
166+
// Prevents the service from being automatically restarted after being killed;
167+
return START_NOT_STICKY
168+
}
169+
140170
override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaSession? {
141171
return binder.session.value?.mediaSession
142172
}

test-app/src/main/java/org/readium/r2/testapp/reader/ReaderRepository.kt

+10-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class ReaderRepository(
5959
private val mediaServiceFacade: MediaServiceFacade =
6060
MediaServiceFacade(application)
6161

62+
fun isEmpty() =
63+
repository.isEmpty()
64+
6265
operator fun get(bookId: Long): ReaderInitData? =
6366
repository[bookId]
6467

@@ -124,7 +127,13 @@ class ReaderRepository(
124127
)
125128
}
126129

127-
return readerInitData.map { repository[bookId] = it }
130+
return readerInitData.map {
131+
repository[bookId] = it
132+
133+
if (it is MediaReaderInitData) {
134+
mediaServiceFacade.openSession(bookId, it.mediaNavigator)
135+
}
136+
}
128137
}
129138

130139
private suspend fun openAudio(
@@ -159,7 +168,6 @@ class ReaderRepository(
159168
)
160169
}
161170

162-
mediaServiceFacade.openSession(bookId, navigator)
163171
val initData = MediaReaderInitData(
164172
bookId,
165173
publication,

0 commit comments

Comments
 (0)