开发者

Short, varying interval timer in Android

开发者 https://www.devze.com 2023-02-08 17:34 出处:网络
I am trying to create a kind of a metronome for Android. It shall play some audible beeps at certain intervals to give people a rhythm. Hence, timing is quite critical. Essentially it shall do the fol

I am trying to create a kind of a metronome for Android. It shall play some audible beeps at certain intervals to give people a rhythm. Hence, timing is quite critical. Essentially it shall do the following

(start) Play beep type 1 (about 0.1s long)

Wait for x milliseconds (between 500 and 1000)

Play beep type 2 (about 0.1s long)

Wait for y milliseconds (between 500 and 1000)

Play beep type 3 (about 0.1s long)

Wait for y milliseconds (between 500 and 1000)

Go back to start

The UI activity will have some buttons to adjust the wait intervals.

From reading all the various blogs, tutorials and discussions it seems that I should be using a postdelayed() Runnable and set the delay to my desired wait time x or y. After that I should start playing the sound. This should allow me to not take the length of the wav sound file into account.

Am I roughly on the right track? I realise that the timing will not be perfect as there are other services runnin开发者_如何学JAVAg which might delay the execution of my timers. In order to improve that I'd be happy to use the phone in flight mode or turn of some other services as I don't need anything else when using this app.

Are there any full examples for such code out there? I am a beginner when it comes to Android. My eyperience is more with straight C embedded systems. Putting all the classes and their functions together is quite daunting.

Any help appreciated, Michael


An idea that just came to my mind regarding the waiting between the beeps would be to launch a separate Java thread where you enter in an infinite loop and call Thread.sleep(interval) like

public MyLooper extends Runnable{
   private boolean shouldRun = true;
   private int interval = 1; //ms

   @Override
   public void run(){
      while(shouldRun){
         //play the beep

         Thread.sleep(interval);
      }

   }


   public void stop(){
      this.shouldRun = false;
   }

   public void setInterval(int interval){
      this.interval = interval;
   }
}

When you launch your activity (depending on your needs) usually in the onResume event, you'd create the Java thread. Inside the button clicks you could then adjust the interval by calling the setInterval(...) method.

Could be a possible solution...


This is what you need: Updating the UI from a Timer

EDIT: That tutorial is specifically targeting a UI-update scenario, but it gives a simple description of what you really want: a TimerTask

class UpdateTimeTask extends TimerTask {
   public void run() {
       long millis = System.currentTimeMillis() - startTime;
       int seconds = (int) (millis / 1000);
       int minutes = seconds / 60;
       seconds     = seconds % 60;

       timeLabel.setText(String.format("%d:%02d", minutes, seconds));
   }
}

and

if(startTime == 0L) {
   startTime = evt.getWhen();
   timer = new Timer();
   timer.schedule(new UpdateTimeTask(), 100, 200);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消