Set a callback for MediaPlayer to get called after X seconds - android

I got a MediaPlayer Object and after, lets say, 5 seconds of the songs time a callback should get called.
How is it possible to archive that with the MediaPlayer?
Or is it necessary to create a wrapper, start a new Thread, wait in that Thread for 5 seconds and call the callback function?

MediaPlayer doesn't have any such thing but one way to accomplish this is with Handler.postDelayed(Runnable, long). That will execute a Runnable after a specific delay.
https://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable, long)
Another option is Timer.schedule(TimerTask, long).
https://developer.android.com/reference/java/util/Timer.html#schedule(java.util.TimerTask, long)

Handler is something you are looking for since media player does not have any api for this.
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 2000ms
playMusic()
}
}, 2000);

Related

Interrupting execution of handler/timer if user input occurs Android

I am trying to make feature that will display text on screen and after few sec to disappear. I have managed that with Handler and Timer classes.
The problem is that I need to somehow stop executing these timers if user makes input over keyboard before timer's time pass and rerun timer again to display different data.
I am facing with problem that the view has remaining visible after user input and disappears after 1-2 sec instead after 5 sec.
the codes that I have used:
//do something
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//do something again
}
}, 5000);
}
AND
//do something
new Timer().schedule(new TimerTask() {
#Override
public void run() {
// this code will be executed after 5 seconds
//do something again
}
}, 5000);
Can you help me to solve this problem?
THanks
For timer you can use timer.cancel(); and for handler you can use handler.removeCallbacks(runnable);
So just declare a Runnable runnable as a global variable, then instantiate it as below
handler.postDelayed(runnable = new Runnable() {
#Override
public void run() {
//do something again
}
}, 5000);
this is the runnable you will pass when you removeCallbacks. Similarly you can use this handler.removeCallbacks(null); this will stop all the handlers that have been declared. I would suggest that you declare both the handler and the timer as global variables and only instatntiate them when you are calling the timer tasks.

Add a delay before playing an audio using Android MediaPlayer

I want to play 5 audio's at a go but with different time delays. Like (a.mp3 with a time delay of 2 sec, b.mp3 = 4 sec etc) but i dont know how to do it.
You can use handler.
Handler handler = new Handler();
final static int DELAY = 1000; // one second
public void playAudioWithDelay(){
handler.postDelayed(new Runnable(){
//your code start with delay in one second after calling this method
}, DELAY);
handler.postDelayed(new Runnable(){
//your code start with delay in two seconds after calling this method
}, DELAY * 2);
}

Run a method every 5 minutes BuzzBox SDK

I need to execute a method every 5 minutes. My problem is where? or How? could be to start the application but did not realize that. Any recommendations?
I don't know how BuzzBox works but I would create a Runnable, then call your method inside it every 5 minutes; That of course uses postDelayed method.
Handler handler = new Handler();
private Runnable updateTimerThread = new Runnable() {
public void run() {
//do call your method here; every 5 minutes
customHandler.postDelayed(this, 5000);
}
};
//you can use your handler anywhere you want like this
handler.postDelayed(updateTimerThread, 5000);

Creating a timer

I'm trying to create a timer, that after the timer ends, will call a function...
For example, I have the function Foo. I want to create a timer, that after 1.5 seconds will call it..
Something like :
Timer(Foo(), 2000);
I have found this code :
private Handler handler = new Handler(); // Creating new handler
handler.postDelayed(runnable, 1500); // Creating a timer for 1.5 seconds
and this function :
private Runnable runnable = new Runnable()
{
#Override
public void run()
{
Foo();
handler.postDelayed(this, 1500);
}
};
My problem is, that some times the timer works perfect, usually for the first 2~3 times, and after that, Instead of being a 1.5sec timer, it become something like 0.3sec timer (and the more handler.postDelayed(runnable, 1500); is being called, the less time the timer will last (like, wont wait 1.5sec to call Foo, but much less)
Why is that ?
I know that in C++ if I write Console Applications, I can use Sleep.. Maybe I can just do something like this :
Sleep(1500);
Foo();
Thanks!
Edit: I have answered my own question.
You could use the Timer class in Android, and set a repeating timer, with a initial delay.
Timer timer = new Timer();
timer.schedule(TimerTask task, long delay, long period)
A TimerTask is very much like a Runnable.
See: http://developer.android.com/reference/java/util/Timer.html
I've used 2 timers :
handler.postDelayed(runnable, 1500); // Creating a timer for 1.5 seconds
this created a 1.5sec timer, while inside the timer loop :
private Runnable runnable = new Runnable()
{
#Override
public void run()
{
Foo();
handler.postDelayed(this, 1500);
}
};
I called handler.postDelayed(this,1500); again, which made 2 timers -> causing the time bug.

To stop Java handler

I have a Java handler code which Im using in android to run a Timer...It runs awesome.
I need help to stop this timer(Handler) programmatically...Any simple method to stop this handler,when I exit from the activity in Android???
The Handler code section is :
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run()
{
// Do something after duration
}
}, duration);
How to stop this handler , so that the statement should not be executed after duration time. Or is there any other way to use a delay timer thread which can be handled by ourself???
Keep references to your Runnables, then call removeCallbacks(runnable) on for each Runnable you've added to your handler to remove them.

Categories

Resources