Android 原生带字幕的视频播放器
我目前正在开发一个安卓视频播放器。我创建了一个简单的文本视图来显示当前的字幕。
问题是: MediaPlayer getCurrentPosition() 方法只给我以秒为单位的时间。我需要该值(以毫秒为单位)才能正确地将字幕与电影同步。
getCurrentPosition 应该给我以毫秒为单位的时间,但它给出的时间以秒 * 1000 为单位(每个数字以“000”结尾)。
- 你知道更好的方法吗?
- 如何获取以毫秒为单位的时间?
- 恰好在第二秒(000ms)的时间生成一个事件也可以。
有什么建议吗?
I'm currently developing a video player for android. I've created a simple textview that shows the current subtitle.
The problem is: MediaPlayer getCurrentPosition() method only gives me time in seconds. And I need that value in milliseconds to correctly synchronize the subtitle with the movie.
The getCurrentPosition is supposed to give me the time in milliseconds, but it gives the time in seconds*1000 (every number ends with "000").
- Do you know a better way of doing this?
- How do I get the time in milliseconds?
- Generating an event exactly at the time of second (000ms) would also work.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果有人需要,我做了我的解决方案。
我开发了字幕模块。
为了获得准确的毫秒数,我为系统和玩家计时器制作了一个同步器。
每播放 20 秒视频,我都会执行此过程:
现在,如果我想要当前电影的毫秒数:
简单,并导致字幕同步;)
In case anyone needs, I did my solution.
I developed the subtitles module.
To get the accurate milliseconds, I made a synchronizer for system and player timers.
Every 20 seconds of video playback I do this procedure:
Now, if I want the current movie milliseconds:
Easy, and resulted in synchronized subtitles ;)
1)解析字幕文件
2)利用CounterTimer类将其设置为player.getDuration(),然后在tick方法中设置您选择的时间间隔(在每个时间间隔的tick上调用)使用player.getCurrentPosition()获取玩家的当前位置; (请记住,这将以毫秒为单位返回时间)。还请记住存储并保留毫秒数。
3)将其转换为您想要的格式,并将其与您获取的时间哈希图或数组相匹配。
4) 使用合适的文本更新覆盖在播放器上的 TextView。
5) 然后取消计时器的持续时间(您可以实现一个具有持续时间的新计时器)。
6) 在 onFinish() 中 Duration 计时器完成后,您可以使用在步骤 2 中保存的 millisecondLeft 时间启动旧计时器。
就是这样;)
1) Parse the subtitle file
2) Make use of CounterTimer class in set it to player.getDuration() then set an interval of your choice in on tick method (which is called on tick of every interval ) get the current position of your player using player.getCurrentPosition(); (Remember this will return you time in millisecond). Please also remember to store and keep millisecondsLeft.
3) convert it into your desired format and match it with time hashmap or array you have taken.
4) Update your TextView which is overlaid on player with the suitable Text.
5) cancel the Timer for duration period then(you can implement a new Timer with duration time).
6) After Duration timer finishes in onFinish() you can start the old timer with millisecondLeft time what you saved in STEP 2.
Thats it ;)