【发布时间】:2015-01-18 07:43:57
【问题描述】:
我使用MediaPlayer 来播放声音,每次一秒都会响起这是我的代码:
// 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();
}
}
一旦计时器达到 43 秒,MediaPlayer 就会按预期停止。
在我像这样调用MediaPlayer release() 之后:
(Button 每次都会被按下,所以我知道它正在被释放
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;
问题是两次播放相同的声音后,它完全停止播放。不知道为什么。 MediaPlayer 只会在计时器第一次启动时播放声音。第二次将达到 45 秒,然后停止工作。第三次等等它根本不会发出声音。请帮忙,谢谢!
这里是所有代码:
// 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;
}
}
}
【问题讨论】:
-
请发布更多代码...您可能没有按预期重置您的变量之一。
-
我添加了所有代码@jim
-
@user2712869: 删除
timeLeft >= 43后条件行为是否相同? -
同样的事情,它最终会在达到 27 秒后停止播放声音。然后拒绝播放,除非我退出应用程序并重新进入
标签: android audio android-mediaplayer