2010-12-06 68 views
1

我想单击按钮的同时用音频文件更新我的ProgressBar。我也使用了根据音频文件长度在几秒钟内运行的定时器。使进度条与计时器同步

我配置了定时器,它根据音频文件的长度很好地增加,但是我的ProgressBar不工作。它给了我NumberFormatException。

我的代码: class MyCount extends CountDownTimer { private int mycount;

 public MyCount(long millisInFuture, long countDownInterval) 
     { 
      super(millisInFuture, countDownInterval); 
     } 

     public void onFinish() 
     { 
      //timeDisplay.setText("done!"); 
     } 
     public void onTick(long millsIncreasing) 
     { 

     mycount++; 
     mystring = formatTime(mycount*1000); 
     myText.setText(mystring); 
     pBar.setProgress(Integer.parseInt(mystring)); (*****) 
     pBar.setProgress(1000 * pBar.getProgress()); 
     /*String myString1 = mystring.substring(0, mystring.lastIndexOf(":")).trim(); 
     String myString2 = mystring.substring(mystring.lastIndexOf(":")+1, mystring.length()).trim();*/ 

我得到了异常,我已经在支架上显示星星。 任何人可以帮我, 感谢 大卫

+0

请问您可以发布`formatTime`函数的代码吗?它会返回一个合成的Integer吗? – 2010-12-06 07:22:56

回答

2

由于你的函数返回一个字符串“12:34”(用:在中)是不是Integer.parse可以分析的数字格式。

您应该保留这将持有该文件的时间长度值(单位:秒)可变

int fileLength 

然后在你的函数:

public void onTick(long millsIncreasing) 
{ 
    int progress = (int)Math.round(millsIncreasing/fileLength) * 100; 
    Bar.setProgress(progress); 

这是假设millsIncreasing参数女士。到目前为止已处理