android - 将进度条与计时器同步

标签 android timer progress-bar

我想在单击按钮时与音频文件同时更新我的​​ ProgressBar。我还使用了根据音频文件的长度(以秒为单位)运行的计时器。

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

我的代码: MyCount 类扩展了 CountDownTimer { 私有(private)诠释我的计数;

        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();*/

我在括号中显示星星的那一行出现了异常。 任何人都可以帮助我, 谢谢 大卫

最佳答案

由于您的函数返回的字符串“12:34”(中间有 :)不是 Integer.parse 可以解析的数字格式。

你应该保留一个变量来保存文件时间长度的值(以秒为单位)

int fileLength

然后在你的函数中:

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

这是假设 millsIncreasing 参数是 ms。处理到此为止。

关于android - 将进度条与计时器同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4364085/

相关文章:

java - java多线程显示进度

java - 如何在不记住密码的情况下以编程方式连接到 Wifi?

android - 需要 Android 的文件保存对话框

java - 在 java 中使用 GSON 解析 JSON 并填充 ListView

c# - 如何以正确的方式处理 BackgroundWorkers

c# - AudioPlayerAgent、计时器和网络服务

c# - 使用定时器更新标签

ios - NSURLConnection expectedContentLength 返回 -1

android - 在单 Activity 架构中为 fragment 设置横向方向

ios - 使用 SDWebImage block 的 ImageView 上的圆形进度条