Reputation: 35
I'm making an Metronome app. I wish to have it count precisely 4 beats in a loop with an accent on the first beat.
Inside Android Studio on my simulator it works fine, but on my actual phone (Galaxy J3 - 2018, Android v9) that time interval is not stable at all it's very random.
I have tried Threads, Coroutines, Handlers but nothing seems to work. I also had a look at the Timer objects, I was trying to copy someone's code on Stack Overflow and refactor it but without any success
This is my code so far (which works on the emulator)
timer = 5000
timeSignatureTop = 4
accentPlayed = false
measureCount = 1
Thread {
while (!stopButtonPressed) {
Log.d("Thread running", "running")
if (!accentPlayed) {
mediaPlayerAccent!!.start()
measureCount++
accentPlayed = true
}
else if (measureCount < timeSignatureTop) {
mediaPlayerBeat!!.start()
measureCount++
}
else if (measureCount == timeSignatureTop) {
mediaPlayerBeat!!.start()
measureCount = 1
accentPlayed = false
}
Thread.sleep(timer.toLong())
}
Log.d("Thread stopped", "stopped")
return@Thread
}.start()
Upvotes: 1
Views: 290