Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AV][Android] Fix AV progress update (#7193)
Co-Authored-By: Bartłomiej Bukowski <bartlomiejbukowski.b@gmail.com>
- Loading branch information
Showing
10 changed files
with
347 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/expo-av/android/src/main/java/expo/modules/av/progress/AndroidLooperTimeMachine.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package expo.modules.av.progress | ||
|
||
import android.os.Handler | ||
|
||
class AndroidLooperTimeMachine : TimeMachine { | ||
|
||
override fun scheduleAt(intervalMillis: Long, callback: TimeMachineTick) { | ||
Handler().postDelayed(callback, intervalMillis) | ||
} | ||
|
||
override val time: Long | ||
get() = System.currentTimeMillis() | ||
} |
67 changes: 67 additions & 0 deletions
67
packages/expo-av/android/src/main/java/expo/modules/av/progress/ProgressLooper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package expo.modules.av.progress | ||
|
||
typealias TimeMachineTick = () -> Unit | ||
|
||
interface TimeMachine { | ||
fun scheduleAt(intervalMillis: Long, callback: TimeMachineTick) | ||
val time: Long | ||
} | ||
|
||
typealias PlayerProgressListener = () -> Unit | ||
|
||
class ProgressLooper(private val timeMachine: TimeMachine) { | ||
|
||
private var interval = 0L | ||
private var nextExpectedTick = -1L | ||
private var waiting = false | ||
|
||
private var shouldLoop: Boolean | ||
get() = interval > 0 && nextExpectedTick >= 0 && !waiting | ||
set(value) { | ||
if (!value) { | ||
interval = 0L | ||
nextExpectedTick = -1L | ||
waiting = false | ||
} | ||
} | ||
|
||
private var listener: PlayerProgressListener? = null | ||
|
||
fun setListener(listener: PlayerProgressListener) { | ||
this.listener = listener | ||
} | ||
|
||
fun loop(interval: Long, listener: PlayerProgressListener) { | ||
this.listener = listener | ||
this.interval = interval | ||
scheduleNextTick() | ||
} | ||
|
||
fun stopLooping() { | ||
this.shouldLoop = false | ||
this.listener = null | ||
} | ||
|
||
private fun scheduleNextTick() { | ||
if (nextExpectedTick == -1L) { | ||
nextExpectedTick = timeMachine.time | ||
} | ||
if (shouldLoop) { | ||
nextExpectedTick += calculateNextInterval() | ||
waiting = true | ||
timeMachine.scheduleAt(nextExpectedTick - timeMachine.time) { | ||
waiting = false | ||
listener?.invoke() | ||
scheduleNextTick() | ||
} | ||
} | ||
} | ||
|
||
private fun calculateNextInterval() = | ||
if (nextExpectedTick > timeMachine.time) { | ||
interval | ||
} else { | ||
(((timeMachine.time - nextExpectedTick) / interval) + 1) * interval | ||
} | ||
|
||
} |
Oops, something went wrong.