开发者

Android Native Video player with subtitles

开发者 https://www.devze.com 2023-04-11 19:15 出处:网络
I\'m currently developing a video player for android. I\'ve created a simple textview that sho开发者_C百科ws the current subtitle.

I'm currently developing a video player for android. I've created a simple textview that sho开发者_C百科ws 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?


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消