How to check something at regular intervals?

I start here, I have a simple question.

In Android, would it be better to check something at regular intervals?

Please carry me, I will try to explain what I can -

For example, my sound application is very simple, basic action and service. The main action has a user interface with two buttons, start and stop the sound. I press the "Start" button and the sound service starts. Likewise, when I click Stop, the service stops and the sound ends. If isLooping() hardcoded to the truth, there is no problem because the sound never ends unless I find a stop button that stops the audio service and also resets the state of the buttons.

This is a problem now, because I set isLooping() to false so that the sound does not loop. Thus, the sound will stop playing, but the service is still running.

I want to be able to detect when the sound stops, so I can set the states of the user interface buttons. So I need something that always checks if the sound is playing (i.e. Check player.isPlaying() so that I can complete the service and set the on / off state of the buttons.

I understood the binding to the service, so I can access the MediaPlayer controls through my main activity, so I know the code to check if it plays, but WHERE do I put this code all the time so that it checks all the time?

Do I make sense? I know that this is probably very simple. Thanks for any help.

+4
source share
1 answer

You can repeat it with TimerTask and Timer. Code below:

 public final void RepeatSoundFunction(){ t = new Timer(); tt = new TimerTask() { @Override public void run() { mp.seekTo(0); //Reset sound to beginning position mp.start(); //Start the sound t.purge(); //Purge the sound } }; t.schedule(tt, 10*1000); //Schedule to run tt (TimerTask) again after 10 seconds } 

then you install MediaPlayer onCompletionListener and there you put it.

Inside the startup code, you can check other things besides music, I just show an example with audio.

+1
source

All Articles