Skip to content

Instantly share code, notes, and snippets.

@mrexmelle
Last active April 14, 2020 09:17
Show Gist options
  • Save mrexmelle/0650791d36065036b40064460fd04811 to your computer and use it in GitHub Desktop.
Save mrexmelle/0650791d36065036b40064460fd04811 to your computer and use it in GitHub Desktop.
An example of how to track the percentage of multimedia file being played.
import java.util.BitSet
const val MIN = 6
const val SEC = 11
fun countSec(m: Int, s: Int): Int {
return (60 * m) + s
}
val COUNT_SEC = countSec(MIN, SEC)
val arr = BitSet(COUNT_SEC)
var start = 0
var end = 0
var isPlayed = false
fun play(sec: Int) {
checkSecIsValid(sec)
isPlayed = true
start = sec
}
fun stop(sec: Int) {
checkSecIsValid(sec)
isPlayed = false
end = sec
arr.set(start, end+1, true)
}
fun scroll(from: Int, to: Int) {
checkSecIsValid(from)
checkSecIsValid(to)
if (isPlayed) {
end = from
arr.set(start, end+1, true)
start = to
}
}
@UseExperimental(kotlin.ExperimentalStdlibApi::class)
fun calculate(arr: BitSet, totalSec: Int) : Double {
val longArray = arr.toLongArray()
var bc = 0;
for (l in longArray) {
bc += l.countOneBits()
}
return (bc.toDouble() / totalSec) * 100
}
fun display(arr: BitSet) {
println("(start,end): (" + start + ", " + end + ")");
for(i in 0..COUNT_SEC) {
print(if (arr.get(i)) 1 else 0);
}
println("");
}
fun checkSecIsValid(sec: Int) {
if (sec < 0 || sec > COUNT_SEC) {
throw IllegalArgumentException(sec.toString() + " is out of (0.." + COUNT_SEC + ")")
}
}
fun main() {
println("Arr count: " + arr.size())
play(0)
display(arr)
stop(14)
display(arr)
scroll(14, 40)
display(arr)
play(40)
display(arr)
stop(42)
display(arr)
scroll(42, 1)
display(arr)
play(1)
display(arr)
stop(300)
display(arr)
println("Calc: " + calculate(arr, COUNT_SEC) + " %")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment