Add a delay before playing an audio using Android MediaPlayer - android

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);
}

Related

How to execute a function with a delay of a few seconds in Android programming

I have two functions. The first function takes 8 seconds and the second function takes 5 seconds. I want the second function to run with a delay of 3 seconds and the program not to stop and the first function not to stop.
How can I do this?
Please help me.
Use the Handler method :
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
yourfuction(); //This function will only work after 3 second
}
}, 3000);
Handler handler = new Handler();
Runnable myRunnable = new Runnable() {
public void run() {
// Things to be done
callYourFunction();
handler.postDelayed(this, 3000); //after every 3 sec call your function
}
};
handler.postDelayed(myRunnable, 3000);

Set a callback for MediaPlayer to get called after X seconds

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);

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.

How to run some code for 20 seconds in android

I want to run some code for 20 seconds precisely. It is similar to a loop but instead of having a variable I have time (in seconds).
I should have a time condition like this:
do
{ variable++ }
while (sec < 20)
How it is possible to do this in Android??
My application should run this 20 sec code after the user presses a button.
You can use the Handler class in Android on a runnable and then use the postDelayed() method. That way you will be able to update the UI during that 20 seconds on the progress of the thread. A good example of this is hear. Your code might look something like this ...
Handler handler = new Handler();
final Runnable r = new Runnable(){
public void run() {
//Do thing after 20 sec
}
};
handler.postDelayed(r, 20000);

Runnable is executing slower than expected

I'm using a runnable in my Android app to update a countdown timer, as shown in the code below. It appears to work but I noticed my timer takes a few seconds longer than expected. For example, if it's supposed to count down 3 minutes, it takes 3 minutes and 5 seconds. I tried using a timer in a service to manage the countdown display in the main activity. The timer/service worked as expected.
Why doesn't runnable/postDelayed() run for the correct amount of time? Is postDelayed() timing reliable? The runnable decrements a variable then uses it to update an EditText with setText(). Does setText() take too long (a small fraction of a second), so the runnable really runs every 1.x seconds?
Handler handler = new Handler();
Runnable r = new Runnable() {
public void run() {
// decrement the time remaining and update the display
handler.postDelayed(this, 1000);
}
};
...
// start the runnable
handler.postDelayed(r, 1000);
Your code is kinda sorta designed to be inaccurate because you are not accounting for the time taken by the guts of the runnable. You might get improved results by doing something like
public void run(){
startTime = System.currentTimeMillis();
// compare expectedTime to startTime and compensate
// <guts of runnable goes here>
// now wrap it up...
delay = 1000 - (System.currentTimeMillis() - startTime);
if (delay < 0)
delay = 0;
expectedTime = System.currentTimeMillies() + delay;
handler.postDelayed(this, delay);
}
What about using CountDownTimer? I used this for same tasks several times and haven’t met this kind of problem.

Categories

Resources