根据音乐播放进度实时更新音乐播放器播放进度条

首先要知道 进度条和Toast是可以在子线程跟新和显示的,因为他们做了内部处理(用handle把信息传递到主线程,在主线程更新,感兴趣的去搜一下)。

具体思路是:

首先,定义一个接口(不要定义内部接口)用来更新seekbar,代码如下

public interface InitplayingPop {
   
void initPlayingPop(int progress);
}

然后,在服务定义一个不断执行的子线程去得到当前音乐的播放时间,并且执行这个接口(在接口不为null的情况下),代码如下

 class SeekBarThread implements Runnable {

    @Override

    public void run() {

        int i = 5;

        while (i > 0) {

            try {

                int currentMent = (int) getCurrentTime();

                         if (initplayingPop != null) {

                    initplayingPop.initPlayingPop(currentMent);

                    }

                Thread.sleep(500);
}

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        }

        Log.w(TAG, "run: 子线程开始");

    }

   }
然后在主界面实现该接口,并把该接口给服务,完成接口回调,代码如下
musicService.setInitplayingPop(new InitplayingPop() {

    @Override

    public void initPlayingPop(int progress) {

        popWinPlaying.setProgressToSeekBar(progress);

    }

});
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值