媒体播放器 - 暂停第二个音频抛出异常

时间:2018-02-19 03:13:10

标签: java android

我正在使用沙漏库进行倒计时。让我们说5分钟。在那段时间里,我一遍又一遍地播放相同的5个音频文件。这很有效。问题在于暂停。当它在第一个音频。它暂停并恢复正常。它到达下一首歌后,我暂停它,它会抛出IllegalStateException。

代码有点难看。在onCompletion中,它一直播放文件,直到它结束。然后我调用playAudio()并且循环开始直到小时玻璃达到零。

pause.setOnClickListener(新的View.OnClickListener()是一个浮动操作按钮,我在暂停图标和播放图标之间切换

Java类代码

    tracks[0] = R.raw.audio0;
    tracks[1] = R.raw.audio1;
    tracks[2] = R.raw.audio2;
    tracks[3] = R.raw.audio3;
    tracks[4] = R.raw.audio4;

    countDown = (TextView) findViewById(R.id.txtCountdown);
    cancel = (FloatingActionButton)findViewById(R.id.cancelMeditation);
    pause = (FloatingActionButton)findViewById(R.id.pauseMeditation);

    Intent intent = getIntent();
    check = intent.getIntExtra("DURATION", -1);
    milliseconds = check * 60000;



    countDown.setText("" + String.format("%d min",
            TimeUnit.MILLISECONDS.toMinutes(milliseconds)));


    pause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(hourglass == null){
                startTimer(milliseconds);
                currentTrack = 0;
                isTimerDone = false;
                playAudio();
                pause.setImageResource(R.drawable.ic_pause);
                return;
            }
            if(hourglass.isPaused()){
                hourglass.resumeTimer();
                pause.setImageResource(R.drawable.ic_pause);
                isPaused = false;
                mediaAffyPlayer.seekTo(length);
                mediaAffyPlayer.start();

            }else if(hourglass.isRunning()){
                hourglass.pauseTimer();
                pause.setImageResource(R.drawable.ic_play1);
                isPaused = true;
                if(mediaAffyPlayer != null) {
                    mediaAffyPlayer.pause();
                    length = mediaAffyPlayer.getCurrentPosition();
                }

            } else{
                startTimer(milliseconds);
                playAudio();
            }

        }
    });


    cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

}


public void startTimer(long milliseconds){
    hourglass = new Hourglass(milliseconds, 1000) {
        @Override
        public void onTimerTick(long timeRemaining) {

            countDown.setText("" + String.format("%d min, %d sec",
                    TimeUnit.MILLISECONDS.toMinutes(timeRemaining),
                    TimeUnit.MILLISECONDS.toSeconds(timeRemaining) -
                            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(timeRemaining))));
            if(TimeUnit.MILLISECONDS.toMinutes(timeRemaining) ==0){
                return;
            }
        }

        @Override
        public void onTimerFinish() {
            isTimerDone = true;
        }
    };

    hourglass.startTimer();

}


private void playAudio(){

    if(mediaAffyPlayer == null){
        mediaAffyPlayer = MediaPlayer.create(getApplicationContext(), tracks[0]);
        mediaAffyPlayer.setOnCompletionListener(this);
        mediaAffyPlayer.start();
        isPaused = false;
        return;
    }
    if(mediaAffyPlayer != null) {
        mediaAffyPlayer.release();
        mediaAffyPlayer = MediaPlayer.create(getApplicationContext(), tracks[0]);
        mediaAffyPlayer.setOnCompletionListener(this);
        isPaused = false;
        mediaAffyPlayer.start();
    }


    mediaAffyPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
        public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
            return false;
        }
    });
}


public void onCompletion(MediaPlayer mediaPlayer) {


    if (currentTrack < tracks.length - 1) {

        if(isTimerDone){
            countDown.setText("" + String.format("%d min",
                    TimeUnit.MILLISECONDS.toMinutes(milliseconds)));
            pause.setImageResource(R.drawable.ic_play1);
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer=null;
            hourglass = null;
            return;
        }

        currentTrack++;
        mediaPlayer.release();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), tracks[currentTrack]);
        mediaPlayer.setOnCompletionListener(ShowMeditationActivity.this);
        mediaPlayer.start();

    } else {
        if(!isTimerDone){
            currentTrack = 0;
            playAudio();
        }
    }
}

异常

 java.lang.IllegalStateException
 at android.media.MediaPlayer._pause(Native Method)
 at android.media.MediaPlayer.pause(MediaPlayer.java:1512)

将其抛出mediaAffyPlayer.pause();在hourglass.isRunning()

我知道代码是丑陋的,但我只是认为当它播放第二首曲目时,媒体播放器对象已经改变,这就是它抛出错误的原因。我只是不知道如何解决它。

任何帮助都表示赞赏,因为我已经烧了好几个小时试图解决它。 感谢

编辑 我解决了这个问题。

我在OnCompletion中注释了创建和播放媒体播放器的代码。我调用了playAudio()并将代码插入其中。

     private void playAudio(){

    if(mediaAffyPlayer == null){
        mediaAffyPlayer = MediaPlayer.create(getApplicationContext(), tracks[0]);
        mediaAffyPlayer.setOnCompletionListener(this);
        mediaAffyPlayer.start();
        isPaused = false;
        return;
    }
    if(mediaAffyPlayer != null) {
        currentTrack++;
        mediaAffyPlayer.release();
        mediaAffyPlayer = MediaPlayer.create(getApplicationContext(), tracks[currentTrack]);
        mediaAffyPlayer.setOnCompletionListener(ShowMeditationActivity.this);
        mediaAffyPlayer.start();

    }

1 个答案:

答案 0 :(得分:0)

isPaused()代码

的问题

试试这个!!

 try{
    if (mediaAffyPlayer.isPlaying()) {
      mediaAffyPlayer.pause();
}
}catch(Exception we){
    we.printStackTrace();
}