2017-07-10 55 views
0

从复位定时器,我有一个当前定时器和总时间textViews一个简单的音乐播放应用程序以及搜索条。好像在我暂停音乐,getDuration和getCurrentPosition都返回0,这就是为什么当前的时间和总时间的文本都去0和搜索栏重置到开始的为好。当我恢复时,一切都会回到原来的位置,好像没有问题。为什么getDuration和getCurrentPosition在暂停时返回0?有没有解决方法?防止搜索栏和暂停

我试图通过设置可见性去创建一个新的解决方案,并创建一个新的两个textViews和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); 
} 

回答

0

我认为这是一个系统错误。我只由当getCurrentPosition()输出0。

实施例的run()不更新搜索条做一种解决方法:

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)); 
    } 
+0

我想,并且它会导致搜索条至根本无法启动,当电流在一首歌曲的开头,时间必须为0 – tiandrew

+0

@tiandrew也许你会记得你做了多少次,只允许一次。所以添加一个变量,比如'布尔开始= FALSE;'然后将其设置在run()方法被执行以真正在第一时间和改变'如果(currentTime的== 0)''到如果(currentTime的== 0 &&开始)' 。 **注:必须后设置开始真正的,如果(当前... ** – Johey

+0

管理保存的搜索条的位置和定时器,但现在不会恢复移动和更新的时候,我继续当你。返回一个线程,你会不得不重新启动它? – tiandrew