From 306649572a94148437f97195daf010ba32cf865e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mickae=CC=88l=20Menu?= Date: Thu, 6 Aug 2020 11:03:23 +0200 Subject: [PATCH 1/2] Fix locator handling in the audiobook player --- .../audiobook/R2AudiobookActivity.kt | 77 +++++++++++-------- .../r2/navigator/audiobook/R2MediaPlayer.kt | 2 +- 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/r2-navigator/src/main/java/org/readium/r2/navigator/audiobook/R2AudiobookActivity.kt b/r2-navigator/src/main/java/org/readium/r2/navigator/audiobook/R2AudiobookActivity.kt index 8601085d..a0a24f42 100644 --- a/r2-navigator/src/main/java/org/readium/r2/navigator/audiobook/R2AudiobookActivity.kt +++ b/r2-navigator/src/main/java/org/readium/r2/navigator/audiobook/R2AudiobookActivity.kt @@ -45,7 +45,7 @@ open class R2AudiobookActivity : AppCompatActivity(), CoroutineScope, IR2Activit title = resource.title, locations = Locator.Locations( fragments = listOf( - "t=${mediaPlayer?.currentPosition?.toInt() ?: 0}" + "t=${TimeUnit.MILLISECONDS.toSeconds(mediaPlayer?.currentPosition?.toLong() ?: 0)}" ), progression = progression ) @@ -61,11 +61,18 @@ open class R2AudiobookActivity : AppCompatActivity(), CoroutineScope, IR2Activit } override fun go(locator: Locator, animated: Boolean, completion: () -> Unit): Boolean { - val resourceIndex = publication.readingOrder.indexOfFirstWithHref(locator.href) - ?: return false + val resourceIndex = publication.readingOrder.indexOfFirstWithHref(locator.href) ?: return false + val mediaPlayer = mediaPlayer ?: run { + pendingLocator = locator + return false + } + + pendingLocator = null currentResource = resourceIndex - mediaPlayer?.goTo(currentResource) + mediaPlayer.goTo(currentResource) + seek(locator.locations) + return true } @@ -126,6 +133,7 @@ open class R2AudiobookActivity : AppCompatActivity(), CoroutineScope, IR2Activit private val backwardTime = 10000 var mediaPlayer: R2MediaPlayer? = null + private var pendingLocator: Locator? = null protected var navigatorDelegate: NavigatorDelegate? = null @@ -149,13 +157,7 @@ open class R2AudiobookActivity : AppCompatActivity(), CoroutineScope, IR2Activit mediaPlayer = R2MediaPlayer(publication.readingOrder, this) - mediaPlayer?.goTo(currentResource) - - currentLocator.value?.locations?.progression?.let { progression -> - mediaPlayer?.seekTo(progression) - seekLocation = currentLocator.value?.locations - isSeekNeeded = true - } + go(pendingLocator ?: publication.readingOrder.first().toLocator()) seekBar?.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { /** @@ -289,27 +291,38 @@ open class R2AudiobookActivity : AppCompatActivity(), CoroutineScope, IR2Activit notifyCurrentLocation() } - private var seekLocation: Locator.Locations? = null - private var isSeekNeeded = false - var isSeekTracking = false - private fun seekIfNeeded() { - if (isSeekNeeded) { - val time = seekLocation?.fragments?.firstOrNull()?.let { - var time = it - if (time.startsWith("#t=") || time.startsWith("t=")) { - time = time.substring(time.indexOf('=') + 1) - } - time + private fun seek(locations: Locator.Locations) { + if (mediaPlayer?.isPrepared == false) { + pendingSeekLocation = locations + return + } + + pendingSeekLocation = null + + val time = locations.fragments.firstOrNull()?.let { + var time = it + if (time.startsWith("#t=") || time.startsWith("t=")) { + time = time.substring(time.indexOf('=') + 1) } - time?.let { - mediaPlayer?.seekTo(TimeUnit.SECONDS.toMillis(it.toLong()).toInt()) - } ?: run { - seekLocation?.progression?.let { progression -> - mediaPlayer?.seekTo(progression) - } + time + } + time?.let { + mediaPlayer?.seekTo(TimeUnit.SECONDS.toMillis(it.toLong()).toInt()) + } ?: run { + val progression = locations.progression + val duration = mediaPlayer?.duration + if (progression != null && duration != null) { + mediaPlayer?.seekTo(progression * duration) } - seekLocation = null - isSeekNeeded = false + } + } + + private var pendingSeekLocation: Locator.Locations? = null + var isSeekTracking = false + + private fun seekIfNeeded() { + pendingSeekLocation?.let { locations -> + seek(locations) } } @@ -398,11 +411,9 @@ open class R2AudiobookActivity : AppCompatActivity(), CoroutineScope, IR2Activit } index++ } - seekLocation = locator.locations + pendingSeekLocation = locator.locations } - isSeekNeeded = true - mediaPlayer?.goTo(currentResource) play_pause!!.callOnClick() diff --git a/r2-navigator/src/main/java/org/readium/r2/navigator/audiobook/R2MediaPlayer.kt b/r2-navigator/src/main/java/org/readium/r2/navigator/audiobook/R2MediaPlayer.kt index af492053..2838054b 100644 --- a/r2-navigator/src/main/java/org/readium/r2/navigator/audiobook/R2MediaPlayer.kt +++ b/r2-navigator/src/main/java/org/readium/r2/navigator/audiobook/R2MediaPlayer.kt @@ -53,8 +53,8 @@ class R2MediaPlayer(private var items: List, private var callback: MediaPl override fun onPrepared(mp: MediaPlayer?) { toggleProgress(false) this.start() - callback.onPrepared() isPrepared = true + callback.onPrepared() } fun startPlayer() { From 33fb67a81e269e803855fbaa1e689ac02d1291aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mickae=CC=88l=20Menu?= Date: Tue, 11 Aug 2020 15:43:36 +0200 Subject: [PATCH 2/2] Release 2.0.0-alpha.1 --- .github/CODEOWNERS | 1 - CHANGELOG.md | 9 +++++---- r2-navigator/build.gradle | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) delete mode 100644 .github/CODEOWNERS diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS deleted file mode 100644 index d9ce658a..00000000 --- a/.github/CODEOWNERS +++ /dev/null @@ -1 +0,0 @@ -* @aferditamuriqi diff --git a/CHANGELOG.md b/CHANGELOG.md index c75610e3..7c1f9f5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,12 +4,9 @@ All notable changes to this project will be documented in this file. **Warning:** Features marked as *experimental* may change or be removed in a future release without notice. Use with caution. -[unreleased]: https://github.com/readium/r2-navigator-kotlin/compare/master...HEAD -[1.2.0]: https://github.com/readium/r2-navigator-kotlin/compare/1.1.6...1.2.0 - ## [Unreleased] -## [1.2.0] +## [2.0.0-alpha.1] ### Added @@ -35,3 +32,7 @@ All notable changes to this project will be documented in this file. * [Crash when opening a publication with a space in its filename](https://github.com/readium/r2-navigator-kotlin/pull/136). * [Jumping to an EPUB location from the search](https://github.com/readium/r2-navigator-kotlin/pull/111). * The `AndroidManifest.xml` is not forcing anymore `allowBackup` and `supportsRtl`, to let reading apps manage these features themselves (contributed by [@twaddington](https://github.com/readium/r2-navigator-kotlin/pull/118)). + + +[unreleased]: https://github.com/readium/r2-navigator-kotlin/compare/master...HEAD +[2.0.0-alpha.1]: https://github.com/readium/r2-navigator-kotlin/compare/1.1.6...2.0.0-alpha.1 diff --git a/r2-navigator/build.gradle b/r2-navigator/build.gradle index 9cf4d77f..69ce29af 100644 --- a/r2-navigator/build.gradle +++ b/r2-navigator/build.gradle @@ -44,7 +44,7 @@ dependencies { if (findProject(':r2-shared')) { implementation project(':r2-shared') } else { - implementation "com.github.readium:r2-shared-kotlin:1.1.6" + implementation "com.github.readium:r2-shared-kotlin:2.0.0-alpha.1" } implementation 'androidx.appcompat:appcompat:1.3.0-alpha01'