How to set Media player Duration in android - android

i want to play MediaPlayer for 1 second. How to set Duration in this code..
player = MediaPlayer.create(getApplicationContext(), R.raw.beepsound);
player.start();
CountDownTimer Timer = new CountDownTimer(1000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
player.start();
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
player.stop();
}
};
Timer.start();

You don't need to do anything on the onTick method.
Try this code:
player = MediaPlayer.create(getApplicationContext(), R.raw.beepsound);
player.start();
CountDownTimer timer = new CountDownTimer(1000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
// Nothing to do
}
#Override
public void onFinish() {
if (player.isPlaying()) {
player.stop();
player.release();
}
}
};
timer.start();
If you look at the constructor:
public CountDownTimer (long millisInFuture, long countDownInterval)
millisInFuture - The number of millis in the future from the call to start() until the countdown is done and onFinish() is called.
So onFinish() will be called after 1 second (1000 millisecond = 1second).
Ref: http://developer.android.com/reference/android/os/CountDownTimer.html#CountDownTimer(long, long)

You could use TimerTask to schedule a MediaPlayer.stop() to run after 1 secs.
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
MediaPlayer.stop()
}
};
timer.schedule(doAsynchronousTask, 0, 1000); //execute in every 20000 ms
}
can u try this one
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if (mediaPlayer.isPlaying())
mediaPlayer.stop();
}
}, timeout);

Related

How to stop MediaRecorder after certain time in Android?

I want to stop recording a video after a certain time e.g. 5 seconds.
Do you know how to do that with a MediaRecorder?
You can use a handler to step the recording after that time.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
recorder.stop();
}
}, DELAY);
Regarding the timer:
int t = 0;
handler.postDelayed(new Runnable() {
#Override
public void run() {
textView.setText(getString(R.string.formatted_time, t));
if(++t<10) {
handler.postDelayed(this, 1000);
}
}
}, 1000);
Where formatted_time is something like that:
<string android:name="formatted_time">%d seconds</string>
mCountdowntimer=new CountDownTimer(countdownPeriod, 1000) {//countdown Period =5000
public void onTick(long millisUntilFinished) {
textView.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mediaplayer.stop(); }
}.start();
and must not forget to call release(); after calling stop();

How to stop countDownTimer when a variable reaches a certain value

What I am trying to do basically is that I want my countDownTimer to run 60 times , i want to do this by setting the value of a variable to 0 and stopping the timer when the variiable reaches the value of 60.
i=0;
new CountDownTimer(3600000,60000) {
public void onTick(long millisUntilFinished) {
imageClock.setImageResource(image1stRec[i++]);
}
public void onFinish() {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.alarm);
mp.start();
}
}.start();
Below method help you to shecdule a countdown for 60 times
new CountDownTimer(60000,1000) {
public void onTick(long millisUntilFinished) {
imageClock.setImageResource(image1stRec[i++]);
}
public void onFinish() {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.alarm);
mp.start();
}
}.start();

Timer stop after one minute

I made a Timer and I want to stop it when it reaches to 60 seconds(1 min).
here's the code:
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
myTextView.setText("timer=" + String.valueOf(TimeCounter));
TimeCounter++;
}
});
}
}, 0, 1000);
int I=60;
if (TimeCounter == I) {
-------------- stop the timer here ----------------
}
}
how can I do it?
you will probably need to move the stopping condition inside your task... so it looks more less like this:
final Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
if (TimeCounter == I) {
t.cancel();
return;
}
myTextView.setText("timer=" + String.valueOf(TimeCounter));
TimeCounter++;
}
});
}
}, 0, 1000);
Remember to define earlier:
int I=60;
Also, you would probably need to mark Timer as final (just as I did in code).
try below code to stop timer :-
if (TimeCounter == I) {
t.cancel();
}
also see below link:-
How to stop immediately the task scheduled in Java.util.Timer class
You can use CountDownTimer, maybe this is a simpler solution:
int Time = 0;
public class OneMinuteCountDownTimer extends CountDownTimer {
public OneMinuteCountDownTimer (long startTime, long interval) {
super(startTime, interval);
}
#Override
public void onFinish() {
myTextView.setText("timer="+Time+" time finished");
Time=0;
}
#Override
public void onTick(long millisUntilFinished) {
Time++;
myTextView.setText("timer="+Time);
}
}
}
Make the CountDownTimer global and call it when You want to start the CountDownTimer:
private OneMinuteCountDownTimer countDownTimer;
private final long startTime = 60 * 1000;
private final long interval = 1 * 1000;
inside onCreate (EDIT):
countDownTimer = new OneMinuteCountDownTimer(startTime,interval);
countDownTimer.start();
and cancel it, if You want to cancel it before one minute:
countDownTimer.cancel();

Android repeat song

I want to repeat the song perodic by 1minutes.
when the song finish playing wait 1 minute and restart
Please help
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
mPlayer.start();
mPlayer.setLooping(true);
}
});
}};
timer.schedule(task, 0, 60000);
You need to subscribe to the completion event in order to know when the song finishes.
Once the event is fired, than, start your delayed execution (no need for timer).
final Handler mHandler = new Handler();
mPlayer.setOnCompletionListener(new OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
mHandler .postDelayed(new Runnable() {
#Override
public void run() {
mPlayer.start();
}
}, 60000);
}
});

I want to play Media Player after 10 seconds

I want to play media player after 10 seconds time delay in android.
player = MediaPlayer.create(getApplicationContext(), R.raw.sleep);
player.start();
You can try something like below: The trick is to start the player after 10 seconds by using a CountDownTimer
player = MediaPlayer.create(getApplicationContext(), R.raw.beepsound);
CountDownTimer timer = new CountDownTimer(10000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
player.start();
}
};
timer.start();
Don't forget to stop & release the player when done with it.
Another approach is with Handler
Handler h = new Handler();
h.postDelayed(new Runnable() {
public void run(){
player.start();
}
}, 10 * 1000);

Categories

Resources