Android Native Video player with subtitles

人走茶凉 提交于 2019-12-20 14:41:48

问题


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").

  1. Do you know a better way of doing this?
  2. How do I get the time in milliseconds?
  3. Generating an event exactly at the time of second (000ms) would also work.

Any suggestions?


回答1:


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:

  1. Ask the mediaplayer for current seconds (got s seconds)
  2. Ask the mediaplayer for current seconds until result is s+1 (s0)
  3. Get the system time (milliseconds) for that time (m0)

Now, if I want the current movie milliseconds:

  1. Get the current system time (millisecons) (m1)
  2. Movie real time is: s0+(m1-m0)

Easy, and resulted in synchronized subtitles ;)




回答2:


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 ;)



来源:https://stackoverflow.com/questions/7714121/android-native-video-player-with-subtitles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!