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
相关文章推荐
- 极光推送步骤(ios)
- Freemaker 自定义指令和函数
- 高cpu排查
- 数组与ArrayList的关联与区别
- XUnit学习
- Cardboard虚拟现实开发初步(三)
- Linux常用命令
- Simple DirectMedia Layer----一套开源的跨平台多媒体开发库
- 基于Qt的Http编程基本原理
- 20个Linux服务器性能调优技巧
- 谈谈毕业后第一份工作和追女生的问题
- 打印 SpringMVC中所有的接口URL
- 免费开源的boostrap模板
- 软件团队作业列表1
- 软件工程作业列表3
- c++ 读入字符的函数简介
- 第7周 指针与字符串
- css px转rem工具,支持生产整个css文件统一修改
- 任意输入一个数,打印对应的空心实心金字塔以及菱形
- [LeetCode] Integer Break 整数拆分