Accurate metronome for android

The answer to this question says that we can make a reliable and accurate metronome on Android using AudioTrack . We can use MediaPlayer , SoundPool , Thread and Timer , but they always cause a delay. Instead of generating synthesized sound using AudioTrack , how can we achieve the same effect with custom audio files?

+8
java android
source share
1 answer

You can try to create your own time counter using System.nanoTime() , when you need accuracy, you can always use this.

public static long nanoTime()

Returns the current value of the exact available system timer, in nanoseconds. This method can be used to measure elapsed time and is not related to any other concept of system time or wall clocks. The return value represents nanoseconds from some fixed but arbitrary time (possibly in the future, so the values ​​can be negative). This method provides nanosecond accuracy, but not necessarily nanosecond accuracy. There is no guarantee on how often the values ​​change. Differences in consecutive calls exceeding approximately 292 years (263 nanoseconds) will not accurately calculate elapsed time due to numerical overflow.

For example, to determine how long it takes to execute code:

long startTime = System.nanoTime (); // ... code being measured ... long ratedTime = System.nanoTime () - startTime;
Returns: current value of the system timer, in nanoseconds. Since: 1.5

A source:

Oracle Documentation https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/System.html#nanoTime ()

+4
source share

All Articles