Hello,
I am working on a hobby media player on Windows using libVLC 2.0.1 and I have a specific question on how to determine the duration of the media being played.
In my player, I use a slider control to model the duration and the progress of the media to be played. Currently I rely on the "libvlc_MediaPlayerLengthChanged" callback event to determine the duration. Specially, when this event is triggered as soon as the media is played, then I call "libvlc_media_player_get_length" to retrieve the length of the media and set the slider to that duration. I have experimented with many media format, like mpg, flv, mov, mp4 etc, they all work alright in the sense that the "libvlc_MediaPlayerLengthChanged" callback event is triggered once or sometimes a few times at the beginning of the playing and then no longer triggered while playing the rest of the media. However I encounter a problem when playing some mp3 files, the "libvlc_MediaPlayerLengthChanged" event will be triggered many times, not only at the beginning of the playing and sometimes even in the middle of the playing. This becomes a problem as it keeps resetting the the slider control.
Here is some info on one of the mp3 files I played for example. It has a constant Bitrate: 128 kbps, Sample Rate: 44100 and Number of frames : 131187, ie. about 57 minutes.
When that file is played, here is a list of "libvlc_MediaPlayerLengthChanged" with different length retrieved, in milliseconds"
3426926
3426979
3426960
3427033
3427006
...
3426952
3426979
3426952
3426979
...
As illustrated above, after initial 4 or 5 seconds, it will stabilize at 3426952 for a while but then from time to time it will change again. One obvious solution is to calculate the delta between each consecutive length changes, if the delta is less than one seconds (1000 ms) then the change can be ignored and this solution works fine. But my question is if this is the right way of doing it ? Would this solution work for variable bitrate mp3 files ? As I tried playing the same file using VLC player, it does not appear to suffer this issue and the duration stay the same. So I wonder how it is done in VLC player.
Has anyone experienced the same issue and how you solved it ?
Thanks a lot.