Reputation: 417
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
Upvotes: 1
Views: 673
Reputation: 5958
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.
Upvotes: 1