android - 多次使用后Media Player停止工作

标签 android audio android-mediaplayer

我使用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;

    }

    }
}

最佳答案

问题出在以下代码中:

                @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();
                    }

                }

现在,在每个“滴答声”(1000毫秒)上,它都将创建一个新的MediaPlayer并启动它,直到倒数计数小于43,这意味着您正在启动17个MediaPlayer s-除了最后一个之外,不会释放它们。因此,您可能会导致MediaPlayer出现内存问题

由于您使用的是重复声音,因此您只需在MediaPlayer中创建onCreate,然后在每个刻度上重置/重播它即可。您不需要重新创建/发行/等。

关于android - 多次使用后Media Player停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27976788/

相关文章:

android - 当播放不止一种声音时如何停止媒体播放器?

java - Android - 从另一个类调用时 MediaPlayer 不会释放

android - AudioTrack : AudioFlinger could not create track, 状态:-32

java - Java 中的 ChrW 和 AscW 等效项(使用 Android SDK)

播放声音时出现 Javafx NullPointerException

c# - 为什么我的混音器有时在播放时按 A/D 会调节音量?

android - 两次绘制相同的矩形看起来会有所不同

android - 在更改显示缩放的 Android N 上以 px 为单位获取屏幕大小

python - 如何在某些C代码执行某个线程时保持python脚本打开?

android - 媒体 Controller 隐藏但媒体播放器继续播放