android - 播放音频时暂停计时器

标签 android audio timer

我添加了“pause.Timer = true;”在媒体播放器代码和“pauseTimer = false;”之前媒体播放器编码后输入,但应用程序显示在音频阅读问题时计时器仍在播放。问题是简单的问题总和问题。我的代码错了吗?

这些是我的代码;

}。开始();}

    private int getScore(){
        String scoreStr = scoreTxt.getText().toString();
        return Integer.parseInt(scoreStr.substring(scoreStr.lastIndexOf(" ")+1));
    }

    private void chooseQuestion(){
        answerTxt.setText("= ?");
        QnNum = random.nextInt(QnsList.length);

    final int DELAY_MS = 1000;


        question.setText("Qn "+(questionNumber));

        pauseTimer = true;

        qn = MediaPlayer.create(this, QnsList[QnNum]);
        qn.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer qn) {
            qn.stop();
            qn.reset();
            qn.release();
            qn = null;

        }
    });

        if (leftTimeInMillisecondsGlobal == 0) {
            if (countDownTimer != null) {
                countDownTimer.cancel();
            }
            setTimer(originalTimerTimeInMilliSeconds);
        } else {
            if (countDownTimer != null) {
                countDownTimer.cancel();
            }
            setTimer(leftTimeInMillisecondsGlobal);
        }

        pauseTimer = false;

        startTimer();

        qn.start();}

最佳答案

尽管我不知道您的计时器如何工作,但仍建议解决方案。

移动pauseTimer = false;在MediaPlayer的onCompletionListener内部。

例如:

  private void chooseQuestion(){
    answerTxt.setText("= ?");
    QnNum = random.nextInt(QnsList.length);

final int DELAY_MS = 1000;


    question.setText("Qn "+(questionNumber));

    pauseTimer = true;

    qn = MediaPlayer.create(this, QnsList[QnNum]);
    qn.setOnCompletionListener(new OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer qn) {
        qn.stop();
        qn.reset();
        qn.release();
        qn = null;
        pauseTimer = false;


    }
});

    if (leftTimeInMillisecondsGlobal == 0) {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        setTimer(originalTimerTimeInMilliSeconds);
    } else {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        setTimer(leftTimeInMillisecondsGlobal);
    }


    startTimer();

    qn.start();

}

关于android - 播放音频时暂停计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31016570/

相关文章:

android - 在黑莓上安装 .jad 文件出现错误 907 无效/缺少 cod 文件

android - 如何在 Android JUnit 测试用例中调用 Button.performClick?

c++ - 设置 libsndfile 库生成的 ogg 文件的比特率

linux - 似乎无法在 linux 上安装 id3v2,似乎没有任何错误

c++ - STL 优先队列 - 删除项目

Android 控制台显示 : W/art: Attempt to remove non-JNI local reference

android - 从蓝牙低功耗 GATT 特性中检索大型 32 位无符号整数

audio - 低功耗蓝牙中的语音功能选项

c# - 如何动态设置 system.timer 的名称

c# - 清除控件后,我应该如何停止控件中的计时器?