Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 防止SeekBar和计时器在暂停时重置_Java_Android_Timer_Seekbar - Fatal编程技术网

Java 防止SeekBar和计时器在暂停时重置

Java 防止SeekBar和计时器在暂停时重置,java,android,timer,seekbar,Java,Android,Timer,Seekbar,我有一个seekBar和一个当前计时器以及一个简单的音乐播放应用程序的总时间文本视图。当我暂停音乐时,getDuration和getCurrentPosition都返回0,这就是为什么当前时间和总时间文本都变为0,seekBar也重置为开始。当我重新开始时,一切都回到了原来的状态,好像没有什么问题似的。为什么getDuration和getCurrentPosition在暂停时返回0?有解决办法吗 我试图通过将visibility设置为gone并创建一组新的两个TextView和seekBar来创

我有一个seekBar和一个当前计时器以及一个简单的音乐播放应用程序的总时间文本视图。当我暂停音乐时,getDuration和getCurrentPosition都返回0,这就是为什么当前时间和总时间文本都变为0,seekBar也重置为开始。当我重新开始时,一切都回到了原来的状态,好像没有什么问题似的。为什么getDuration和getCurrentPosition在暂停时返回0?有解决办法吗

我试图通过将visibility设置为gone并创建一组新的两个TextView和seekBar来创建一个jank解决方案,这两个TextView和seekBar可以在暂停和临时显示之前保存时间和seekBar,但它们笨重且有延迟,因此我仍能在一瞬间看到重置项。我用一个按钮处理播放和暂停。如果需要其他信息,请告诉我。下面是我的seekBar处理方法,这两种方法都在onCreate中调用:

/**
 * SeekBar setup and handling
 * TODO: Bug where seekBar sets to 0 when paused. Retains position when resumed.
 */
private void initializeSeekBar() {
    seekBar = (SeekBar) findViewById(R.id.seekBar);
    totalDuration = new TextView(this);
    totalDuration = (TextView) findViewById(R.id.totalDuration);
    currentDuration = new TextView(this);
    currentDuration = (TextView) findViewById(R.id.currentDuration);

    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(final SeekBar seekBar, int progress, boolean fromUser) {
            if (fromUser && musicBound) {
                // update current time on change
                currentDuration.setText(convertedTime(progress * 1000));
                // update seekBar
                seekTo(progress * 1000);
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            musicService.mutePlayer();
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            musicService.unMutePlayer();
            seekTo(getCurrentPosition());
        }
    });
    // start seekBar updating thread
    run();
}

/**
 * Thread that updates seekBar as song plays
 */
@Override
public void run() {
    updateMetadataDisplay();

    if (musicBound) {
        long totalTime = getDuration();
        long currentTime = getCurrentPosition();

        seekBar.setMax(getDuration() / 1000);
        seekBar.setProgress(getCurrentPosition() / 1000);

        // timers
        totalDuration.setText(convertedTime(totalTime));
        currentDuration.setText(convertedTime(currentTime));
    }
    handler.postDelayed(this, 1000);
}

我认为这是一个系统错误。当getCurrentPosition()输出0时,我只需通过不更新seekBar来解决问题

run()中的示例:


我认为这是一个系统错误。当getCurrentPosition()输出0时,我只需通过不更新seekBar来解决问题

run()中的示例:


我试过了,这会导致seekBar根本无法启动,因为当前时间在一个事件开始时必须为0song@tiandrew也许数一数你做这件事的频率,只允许做一次。因此,添加一个变量,如
boolean start=false
然后在第一次执行run()方法时将其设置为true,并将
if(currentTime==0)
更改为
if(currentTime==0&&start)
。注意:在if之后,必须将start设置为true(当前…它成功地保存了seekBar位置和计时器,但现在它不会在我恢复时继续移动和更新时间。当你返回线程时,你是否需要以某种方式重新启动它?我尝试过,它会导致seekBar根本无法启动,因为当前时间在线程开始时必须为0song@tiandrew也许算一下你多久去一次因此,添加一个变量,如
boolean start=false;
,然后在第一次执行run()方法时将其设置为true,并将
if(currentTime==0)
更改为
if(currentTime==0&&start)
。注意:在if之后必须将start设置为true(当前…成功保存seekBar位置和计时器,但现在当我继续时,它不会继续移动和更新时间。当您返回线程时,您是否需要以某种方式重新启动它?
    if (musicBound) {
      long totalTime = getDuration();
      long currentTime = getCurrentPosition();

      if(currentTime == 0)
        return;

      seekBar.setMax(totalTime / 1000);
      seekBar.setProgress(currentTime / 1000);

      // timers
      totalDuration.setText(convertedTime(totalTime));
      currentDuration.setText(convertedTime(currentTime));
    }