-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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 progress update loop.
- Loading branch information
Showing
5 changed files
with
113 additions
and
31 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
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() | ||
} |
68 changes: 68 additions & 0 deletions
68
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,68 @@ | ||
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 changeListener(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 | ||
} | ||
|
||
|
||
} |