简体   繁体   English

TimerTask已经安排好了

[英]TimerTask is already scheduled

I have a MediaPlayer object which plays a local audio file. 我有一个播放本地音频文件的MediaPlayer对象。 I am using a TimerTask to update the position of a SeekBar and text on a TextView while the audio plays. 我正在使用TimerTask在播放音频时更新SeekBar的位置和TextView上的文本。

There is one button. 有一个按钮。 When the user presses the button audio starts. 当用户按下按钮时音频开始。 When they press it a second time the audio stops. 当他们第二次按下它时音频停止。 When they press it a third time to begin playback again the app crashes with the error 当他们第三次按下它再次开始播放时,应用程序会因错误而崩溃

'causedby: TimerTask is scheduled already and InvocationTargetException'.

Here is my code: 这是我的代码:

    public void onAudioClicked(View v) {
    if (mainPlayer == null) {
        mainPlayer = MediaPlayer.create(this, R.raw.session1);
        // Get audio duration and set textview
        int d = mainPlayer.getDuration();
        TextView t = (TextView)findViewById(R.id.totalTime);
        t.setText(String.format("%d:%d", 
                TimeUnit.MILLISECONDS.toMinutes(d),
                TimeUnit.MILLISECONDS.toSeconds(d) - 
                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(d))));

        sb.setProgress(0);
        sb.setMax(mainPlayer.getDuration());
        mainPlayer.start();
        timer = new Timer();
        timer.schedule(task, 1000);
    } else {
        mainPlayer.stop();
        mainPlayer.release();
        mainPlayer = null;
        timer.cancel();
        task.cancel();
        timer = null;
    }
}

    TimerTask task = new TimerTask(){
    public void run(){
        int currentPosition = 0;
        int total = mainPlayer.getDuration();
        sb.setMax(total);
        while (mainPlayer != null && currentPosition < total) {
            try {
                Thread.sleep(1000);
                currentPosition = mainPlayer.getCurrentPosition();
                runOnUiThread(updateControlsRunnable);
            } catch (InterruptedException e) {
                return;
            } catch (Exception e) {
                return;
            }
            sb.setProgress(currentPosition);
        }

    }
};

final Runnable updateControlsRunnable = new Runnable () {
    public void run(){
        int d = mainPlayer.getCurrentPosition();
        TextView t = (TextView)findViewById(R.id.currentTime);
        t.setText(String.format("%d:%d", 
                TimeUnit.MILLISECONDS.toMinutes(d),
                TimeUnit.MILLISECONDS.toSeconds(d) - 
                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(d))));
        }
};

Try this code block please: 请尝试此代码块:

sb.setProgress(0);
sb.setMax(mainPlayer.getDuration());
mainPlayer.start();
timer = new Timer();
MyTask task=new MyTask();
timer.schedule(task,0, 1000);

private class MyTask extends TimerTask {
    public void run() {
        //Your code...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM