VideoView 获取当前播放时间

VideoView 获取当前播放时间

最近在做视频,遇到这么个需求,播放视频中途退出时候记录当前播放的时间,播放视频,最简单的就是VideoView了,但是,官方并没有提供获取当前播放时间的方法,只有个getCurrentPosition()方法,可以获取当前播放的进度。
一般用VideoView时候都会配合MediaController来使用,MediaController就带有显示当前时间和总时间的功能。于是我就通过查看MediaController的源码,找到了如何实现VideoView获取当前时间的方法。

下面直接贴实现的方法吧
//将长度转换为时间
StringBuilder mFormatBuilder;
Formatter mFormatter;
mFormatBuilder = new StringBuilder();
mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
    private String stringForTime(int timeMs) {
        int totalSeconds = timeMs / 1000;

        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours   = totalSeconds / 3600;

        mFormatBuilder.setLength(0);
        if (hours > 0) {
            return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
        } else {
            return mFormatter.format("%02d:%02d", minutes, seconds).toString();
        }
    }

这个就是获取当前时间的方法了

String time=stringForTime(VideoView.getCurrentPosition());
这样就可以通过videoview获取当前播放的时间了

by Lake

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值