Holly
Holly

Reputation: 41

Steady, accurate timing in Android

I'm trying to create a music sequencer app for Android devices and would appreciate some advice as to how to achieve rock-solid timing functionality.

If I pass a Runnable to Handler.postDelayed and specify a delay time of x milliseconds, is that Runnable guaranteed to be executed in exactly x ms time?

If I can't achieve steady and accurate timing with Handler, what other options are open to me?

Thank you in advance

Upvotes: 2

Views: 2978

Answers (2)

Holly
Holly

Reputation: 41

After much googling I managed to find the answers here:

http://masterex.github.com/archive/2012/05/28/android-audio-synthesis.html

Handlers, Thread.sleep() and timers are not the way to go it seems.

Thank you DrA for your answer, it was appreciated - sorry for not acknowledging sooner, I've been away for a while.

Upvotes: 2

DrA
DrA

Reputation: 437

I don't think "hard realtime" programming is possible. The options seem to be to look for events that signal when something has happened. This can be done by creating your own flag, and polling for the value of that flag in the other thread.

You can use a CountDownTimer to specify a particular length of time must pass in millis.

You can use the System clock and make your own timer, by polling on how much time has passed.

There is also a useful class called FileObserver that allows you to watch a particular file, and hear the events like Opened, and Closed. Using those signals you can know that it is time to move to the next operation.

The worst possible bug is that there is a timing issue that occurs on a specific piece of hardware but doesn't happen on most. The problem if that I don't own all of them, and finding the timing issue is tough. I'm hoping that others will have a better - or more - answers for this issue.

Upvotes: 1

Related Questions