MediaPlayer doesn't start when CountDownTimer is finished - android

I have a CountDownTimer like this:
cuentaAtras = new CountDownTimer(mili, 1000) {
public void onTick(long millisUntilFinished) {
long seconds = millisUntilFinished / 1000;
if (seconds <= TIEMPO3){
if (tic1.isPlaying()){
tic1.stop();
tic2.start();
}
} else if (seconds <= TIEMPO && seconds > TIEMPO2 + 10){
tic1.start();
}
}
public void onFinish() {
if (tic2.isPlaying())
{
tic2.stop();
}
ticExp.start();
tic1.reset();
tic2.reset();
ticExp.reset();
}
}.start();
Variables tic1, tic2 y ticExp type is MediaPlayer :
tic1 = MediaPlayer.create(getApplicationContext(), R.raw.tic1);
tic1.setLooping(true);
tic2 = MediaPlayer.create(getApplicationContext(), R.raw.tic3);
tic2.setLooping(true);
ticExp = MediaPlayer.create(getApplicationContext(), R.raw.exp);
When I execute onFinish method, the MediaPlayer ticExp should be played, but this doesn't occur.
I tested write ticExp.start() in method onTick and this works, so the file is not corrupted or something like this.
I don't have idea why in onTick method works, but in onFinish no.
(But the others sentences of code in onFinish are executed without problems)
Definitively, I want that ticExp will be played in onFinish method.
Sorry for my English. Thanks.

You are doing ticExp.reset(); after ticExp.start(); in onFinish(), That means you are actually starting and also resetting(making it idle). Just remove ticExp.reset(); in onFinish().
For the Complete State of MediaPlayer, you can refer this
And one more thing, Your else condition in onTick method will never execute.

Related

Starting MediaPlayer from Timer

I am developing a Battery Alarm App. and I want to play music using media-player in every one minute using the Timer and also want to repeat music 2 times within one minute.
This works fine for the first time when Timer calls the *8playAlarm() method** but after one minute when Timer again call the playAlarm() method it plays music only once.
#Override
public void onCreate() {
super.onCreate();
mStartTime=preferencesAlert.getInt("alert",0); // 0 == immediately
mInterval=preferencesInterval.getInt("interval",1); // 1==1 minute
mSongUri=preferencesSongUri.getString("uri","android.resource://"+getPackageName()+"/raw/celesta");
timer = new Timer();
TimerTask hourlyTask = new TimerTask() {
#Override
public void run () {
playAlarm();
}
};
// schedule the task to run starting now and then every time interval...
timer.schedule (hourlyTask,60000*mStartTime, 60000*mInterval);
}
playAlarm() method
private void playAlarm(){
playerAlarm=MediaPlayer.create(getApplicationContext(),Uri.parse(mSongUri));
playerAlarm.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
repeatMax=preferencesRepeat.getInt("repeat",2);
if(repeatMin<=repeatMax){
repeatMin++;
if(playerAlarm!=null && !playerAlarm.isPlaying()) {
mp.seekTo(0);
mp.start();
}
}else {
mp.reset();
mp.release();
}
}
});
playerAlarm.start();
}
I want to play music two times in every minute but it plays only once
the first time works because your repeatMin is still at its first value but after you did not set it at 0 again in your code
repeatMin = first int value; // you have to reinitialize your repeatMin somewhere when the sound
has been played for the second time
I guess because you are executing method in onCreate which calls only once. Might be you can make it in some background service and call it every minute

How to Pause/Stop MediaPlayer Android at given time programmatically?

I have mediaPlayer for playing sound in android and we want play files based on start time and finishing time
for example :
start 10370 ,end: 14759 [OR] start 4754 ,end: 7836
i used this code for my problem but it's not working perfect
Pause/Stop MediaPlayer Android at given time programmatically
My code :
mediaPlayer.setDataSource(fd);
mediaPlayer.prepare();
mediaPlayer.seekTo(words.getW_start());
mediaPlayer.start();
handler.postDelayed(stopPlayerTask, words.getW_end());
Do you Know why code for these times doesn't work?
I could resolve problem .complete code:
mediaPlayer.setDataSource(fd);
mediaPlayer.prepare();
mediaPlayer.seekTo(words.getW_start());
mediaPlayer.start();
new CountDownTimer(endTime, 10) {
public void onTick(long millisUntilFinished) {
if(MediaPlayerUtility.getTime(mediaPlayer)>=endTime){
mediaPlayer.stop();
}
}
public void onFinish() {
}
}.start();
and MediaPlayerUtility class:
public class MediaPlayerUtility {
public static long getTime(MediaPlayer mediaPlayer) {
long totalDuration = mediaPlayer.getDuration(); // to get total duration in milliseconds
long currentDuration = mediaPlayer.getCurrentPosition(); // to Gets the current playback position in milliseconds
return currentDuration;
}
}

Sound during countdown timer between two numbers

I am making an app where a different sound needs to be played based on the number the countdown timer is on.
// Define CountDown Timer Attributes//
waitTimer1 = new CountDownTimer(60000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
long timeLeft = millisUntilFinished / 1000;
Timer.setText("" + String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
if (timeLeft >= 43) {
mp.start();
}
}
#Override
public void onFinish() {
t1.setEnabled(true);
t2.setEnabled(true);
next.setEnabled(false);
Timer.setText("0:00");
next.setText("Start");
waitTimer1 = null;
}
}.start();
So far I have it so a sound plays from 1:00 to :43 seconds like so:
if (timeLeft >= 43) {
mp.start();
}
I need the next sound to play from 42 seconds to 27 seconds something like this:
if (timeLeft <42,>27) {
mp2.start();
}
Obviously this doesn't work I feel like i'm just typing it in wrong. I need the app to know to start the second sound when the timer hits 43 seconds then stop at 27 seconds. Thanks
You are looking for this:
else if(timeLeft <= 42 && timeLeft > 27)
{
mp2.start();
}
This will loop from 42 through 28 and thus once it ticks to 27 it will stop.
For every condition inside an if statement you need to explicitly tell the compiler what two variables you are comparing. In this case we have 2 conditions:
timeLeft <= 42 and timeLeft > 27
The comparsion between statements can either be Logical Or or Logical And
Logical Or is represented by ||
Logical And is represented by &&
In your case you want to use the logical and statement to compare your two conditions.

Increase the sound in each 5 seconds through Media Player

I want to increase the sound by one level up in each and every 5 seconds meanwhile the sound clip plays.Below is my code:-
MediaPlayer player;
player=MediaPlayer.create(this, R.raw.alarm);
player.setLooping(true);
Is there any method in which i can track each and every 5 minutes when over.How can i achieve this?
I think you can achieve this with java TimerTask, this is an example code, not tested but should work with no big modifications :
Basically you start a task every 5 seconds, in the run() function of the TimerTask you level up your sound level, and when you reach the maxSoundLevel you call cancel() to stop the task.
//Put this in global
int REFRESH_INTERVAL = 5 * 1000; //5 seconds
int maxSoundLevel = 10; // Number of loop to get to max level
int curSoundLevel = 0; //Start at 0 volume level
//Put this after you started the sound
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTimerTask(), 0, REFRESH_INTERVAL);
//Put this after your method
private class MyTimerTask extends TimerTask{
public void run() {
if(curSoundLevel < maxSoundLevel)
{
float logLevel = (float)(Math.log(maxSoundLevel-curSoundLevel)/Math.log(maxSoundLevel));
yourMediaPlayer.setVolume(1-logLevel);
curSoundLevel ++;
}
else {
this.cancel();
}
}
}
If you have more questions feel free to ask me.

Play sound file on Timer

I am displaying a Timer on a screen.
The requirement is I need to play a sound every second when the timer duration is 5 seconds remaining & finally when it reaches 0 (end) , I need to play another sound.
I am using the following code:
public void onTick(long millisUntilFinished) {
long timeLeft = secondsRemaining // I am getting the seconds left here
if (timeLeft <= 5) {
playAlertSound(R.raw.beep);
}
else if(timeLeft == 0){
playAlertSound(R.raw.beep1);
}
public void playAlertSound(int sound) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), sound);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
The problem I am facing using the above code , though the each beep sound duration is less than 1 second , I am getting a continuation of the sound. I want to have separate beep sound for every second starting from the remaining 5 seconds till it reaches zero.
Also , the volume of the sound is too low.
Kindly provide your inputs , whether I need to follow any other logic ?
Thanks in advance.
Warm Regards,
CB
You should make sure looping is disabled using the setLooping function and for the volume issue, you can set the playback volume using: the setVolume function.
But, for your appliction you may want to look into using the SoundPool class instead of the MediaPlayer because it's more suitable for situations like yours when you want to play the same short sound more than once.
With your sample code, I would also reverse the order of the setting the onCompleteListener and starting playback, like this:
MediaPlayer mp = MediaPlayer.create(getBaseContext(), sound);
// move the mp.start() from here..
mp.setOnCompletionListener(new OnCompletionListener() {
// your handler logic here..
});
// and add the setLooping and setVolume calls here..
mp.setLooping(false);
mp.setVolume(1.0, 1.0);
mp.start(); // to here..
I tried this code without the OnCompletionListener, and it worked fine with no looping? I guess I'm not understanding your problem. Also... setVolume(1.0,1.0) isn't correct, you need (float,float) not doubles.

Categories

Resources