Djorn
Djorn

Reputation: 21

Countdown playing Alarm

I really hope you can help me here. Below is a section of my code that successfully runs two count down timers side by side...all I want to do is play a short mp3 file when count down has finished....I have tried many different bits of code but I am struggling to make anything work...an quick wins would be good..

So to round up two timers each need to play sound when finished..

//Declare Start/Stop button
Button btnstart = (Button)findViewById(R.id.btnstart);
Button Button1 = (Button)findViewById(R.id.Button01);

final TextView mCounter1TextField=(TextView)findViewById(R.id.counter1);
final TextView mCounter2TextField=(TextView)findViewById(R.id.counter2);

//Counter 1
final CountDownTimer Counter1 = new CountDownTimer(9000000 , 1000) {
public void onTick(long millisUntilFinished) {
    mCounter1TextField.setText(" " + formatTime(millisUntilFinished));
}

public void onFinish() {
    start();
}

};

//Counter 2
final CountDownTimer counter2 = new CountDownTimer(9000000 , 1000) {
public void onTick(long millisUntilFinished) {
  mCounter2TextField.setText(" " + formatTime(millisUntilFinished));

}

public void onFinish() {
  start();
}


};

//Start Button1
btnstart.setOnClickListener(new OnClickListener() {
 public void onClick(View v) {
  Counter1.start();

   }
});

//Start Button2
 Button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
 counter2.start();

Thanks in advance

Dj

Upvotes: 1

Views: 2710

Answers (1)

Karim
Karim

Reputation: 5308

I assume start() is the function you call to play the sound, right ?

so inside the definition of start(), put the following code :

MediaPlayer mp = MediaPlayer.create(getBaseContext(), sound); //replace 'sound' by your music/sound
mp.start();

Hope this helps!

Edit: trying to be super clear :)

Somewhere in your code, it is written :

public void onFinish() {
    start();
}

This method/function is called when the counter finishes. Inside this function it is written 'start()'

I don't know what this start() does.

In both cases, I suggest you keep it (if it doesn't create an error), and after start(), add playSound() inside the two onFinish() methods.

and then write OUTSIDE of this function, the following:

public void playSound() {

MediaPlayer mp = MediaPlayer.create(getBaseContext(), sound); //replace 'sound' by your    music/sound
mp.start();

}

Upvotes: 3

Related Questions