Skip to content

Commit 70d8bb1

Browse files
authored
Add an Audiobook navigator based on MediaSession (#145)
1 parent c8b5f50 commit 70d8bb1

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

readium/streamer/r2-streamer/src/main/java/org/readium/r2/streamer/container/PublicationContainer.kt

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,8 @@ internal class PublicationContainer(
4343
}
4444
}
4545

46-
override fun dataInputStream(relativePath: String): InputStream = runBlocking {
47-
publication.get(relativePath).stream()
48-
}
46+
override fun dataInputStream(relativePath: String): InputStream =
47+
ResourceInputStream(publication.get(relativePath)).buffered()
4948

5049
private fun Publication.get(href: String) = get(Link(href))
5150
}
52-
53-
54-
/**
55-
* Creates an [InputStream] to read the content.
56-
*/
57-
fun Resource.stream(): InputStream = ResourceInputStream(resource = this)

readium/streamer/r2-streamer/src/main/java/org/readium/r2/streamer/server/handler/PublicationResourceHandler.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ class PublicationResourceHandler : RouterNanoHTTPD.DefaultHandler() {
117117
}
118118

119119
} else {
120-
createResponse(Status.PARTIAL_CONTENT, mimeType, ResourceInputStream(resource, startFrom..endAt), dataLength)
120+
val responseStream = ResourceInputStream(resource, range = startFrom..endAt)
121+
createResponse(Status.PARTIAL_CONTENT, mimeType, responseStream, dataLength)
121122
.apply {
122123
addHeader("Content-Range", "bytes $startFrom-$endAt/$dataLength")
123124
addHeader("ETag", etag)
@@ -165,10 +166,10 @@ class PublicationResourceHandler : RouterNanoHTTPD.DefaultHandler() {
165166
}
166167

167168
private fun responseFromFailure(error: Resource.Exception): Response {
168-
val status = when(error) {
169+
val status = when (error) {
169170
is Resource.Exception.NotFound -> Status.NOT_FOUND
170171
is Resource.Exception.Forbidden -> Status.FORBIDDEN
171-
is Resource.Exception.Unavailable -> Status.SERVICE_UNAVAILABLE
172+
is Resource.Exception.Unavailable, is Resource.Exception.Offline -> Status.SERVICE_UNAVAILABLE
172173
is Resource.Exception.BadRequest -> Status.BAD_REQUEST
173174
is Resource.Exception.Cancelled, is Resource.Exception.OutOfMemory, is Resource.Exception.Other -> Status.INTERNAL_ERROR
174175
}

0 commit comments

Comments
 (0)