Reputation: 417
Im using a MediaPlayer
to play a sound every time a second goes off this is my code:
// 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 = MediaPlayer.create(getApplicationContext(), R.raw.beeb1);
mp.start();
}
}
Once the timer hits 43 seconds the MediaPlayer
stops like its supposed to.
after I call the MediaPlayer
release()
like so:
(The Button
is pressed every single time so I know it is getting released
case R.id.Team1:
Category.team_one++;
number1.setText(String.valueOf(Category.team_one));
t1.setEnabled(false);
t2.setEnabled(false);
next.setEnabled(true);
Timer.setText("1:00");
mp.release();
break;
The problem is after two times of playing the same sound, it just completely stops playing it. Not sure why. The MediaPlayer
will only play the sound the first time the timer starts. The second time it will get to 45 seconds then stop working. The third time and so on it wont make a sound at all. Please help, Thanks!
here is all the code:
// Declare TextView Variable Number One//
protected TextView number1;
// Declare TextView Variable Number Two//
protected TextView number2;
// Declare TextView Variable Timer//
protected TextView Timer;
// Declare TextView Variable Word//
protected TextView word;
// Declare Button Variable Next//
protected Button next;
// Declare CountDown Timer Variable//
private CountDownTimer waitTimer1;
// Declare Button Variable Team One//
protected Button t1;
// Declare Button Variable Team Two//
protected Button t2;
// Declare Media_Player Variable MP//
MediaPlayer mp;
// Where List Starts//
int stringListCounter;
// Shuffle List//
@Override
public void onResume() {
super.onResume();
stringListCounter = randInt(0, 100);
}
// Shuffle List
private int randInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
// What Happens When Activity Starts//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animals);
// Link Button Team One to Activity_Animals//
t1 = (Button) findViewById(R.id.Team1);
t1.setEnabled(false);
t1.setOnClickListener(this);
// Link Button Number One to Activity_Animals//
number1 = (TextView) findViewById(R.id.Number1);
number1.setText(String.valueOf(Category.team_one));
// Link Button Number Two to Activity_Animals//
number2 = (TextView) findViewById(R.id.Number2);
number2.setText(String.valueOf(Category.team_two));
// Link Button Team Two to Activity_Animals//
t2 = (Button) findViewById(R.id.Team2);
t2.setEnabled(false);
t2.setOnClickListener(this);
// Link TextView Timer to Activity_Animals//
Timer = (TextView) findViewById(R.id.Timer);
// Link Button Next to Activity_Animals//
next = (Button) findViewById(R.id.Next);
next.setOnClickListener(this);
// Link TextView Word to Activity_Animals//
word = (TextView) findViewById(R.id.Word);
}
// What Happens When Said Variable Is Clicked//
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Next:
t1.setEnabled(false);
t2.setEnabled(false);
next.setText("next");
if (waitTimer1 == null) {
// 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 = MediaPlayer.create(getApplicationContext(), R.raw.beeb1);
mp.start();
}
}
@Override
public void onFinish() {
t1.setEnabled(true);
next.setEnabled(false);
waitTimer1 = null;
Timer.setText("0:00");
next.setText("Start");
}
}.start();
// Repeat Words//
if (word.getText().toString().equals("Big Foot")) {
stringListCounter = 0;
}
// Change To Next Word//
stringListCounter++;
word.setText(stringIdList[stringListCounter]);
} else{
// Repeat Words//
if (word.getText().toString().equals("Big Foot")) {
stringListCounter = 0;
}
// Change To Next Word//
stringListCounter++;
word.setText(stringIdList[stringListCounter]);
}
break;
case R.id.Team1:
Category.team_one++;
number1.setText(String.valueOf(Category.team_one));
t1.setEnabled(false);
t2.setEnabled(false);
next.setEnabled(true);
Timer.setText("1:00");
mp.stop();
mp.reset();
mp.release();
break;
case R.id.Team2:
Category.team_two++;
number2.setText(String.valueOf(Category.team_two));
t1.setEnabled(false);
t2.setEnabled(false);
next.setEnabled(true);
Timer.setText("1:00");
mp.release();
mp = null;
break;
}
}
}
Upvotes: 0
Views: 1709
Reputation: 10278
The problem is in this code:
@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 = MediaPlayer.create(getApplicationContext(), R.raw.beeb1);
mp.start();
}
}
Right now, on every "tick" (1000 ms) it is creating a new MediaPlayer
and starting it until the countdown is less than 43, which means you are starting 17 MediaPlayer
s - and not releasing any of them except the last one. So you are probably causing memory issues with MediaPlayer
Since you are using a repeated sound, you should simply create your MediaPlayer
in onCreate
and on each tick reset/replay it. You don't need to re-create/release/etc.
Upvotes: 2