Media Player在多次使用后停止工作

时间:2015-01-16 03:18:03

标签: 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;

    }

    }
}

1 个答案:

答案 0 :(得分:2)

问题在于此代码:

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

                }

现在,每一个" tick" (1000毫秒)它正在创建一个新的MediaPlayer并开始直到倒计时小于43,这意味着你开始17 MediaPlayer s - 并且除了最后一个之外不释放它们中的任何一个。因此,您可能会因MediaPlayer

而导致内存问题

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