您的位置:首页 > 其它

VideoView 获取当前播放时间

2016-04-20 11:37 519 查看

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: