I would like to tell the Home Activity to play a mp3 sound when there is 20 minutes left in the CountDown Timer and being relatively new at Eclipse I just can't figure it out, everything I have tried just doesn't work.
public class Home extends Activity {
Button btnStart;
TextView textViewTime;
MediaPlayer mpAudio;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
btnStart = (Button)findViewById(R.id.btnStart);
textViewTime = (TextView)findViewById(R.id.textViewTime);
textViewTime.setText("01:00:00");
final CounterClass timer = new CounterClass(3600000,1000);
btnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
timer.start();
}
});
}
public void tipsandtricks(View view) {
Intent intent = new Intent(this, TipsandtricksActivity.class);
startActivity(intent);
}
public void about(View view) {
Intent intent = new Intent(this, AboutActivity.class);
startActivity(intent);
}
public class CounterClass extends CountDownTimer {
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
textViewTime.setText("Free Transfer Over");
playSound();
}
public void playSound() {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), (R.raw.freetransferover));
mp.start();
}
@Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
System.out.println(hms);
textViewTime.setText(hms);
{
if((TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished)==20) &&
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))==00)
{
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(7000);
{
}
}
}
}
}
}
As you can see in the code (which functions correctly) I've gotten an mp3 sound to play when the CountDown Timer finishes and I figured out how to get the Vibrator to work when there is 20 minutes left. But how to I get a second mp3 sound to play only once when the CountDown Timer hits 20 minutes left?
Any help is greatly appreciated.