Search code examples
androidlibvlc

Utilising LibVLC MediaPlayer.Event.EndReached to reset MediaPlayer when playback finishes


I'm in the process of writing an Android app activity that houses a LibVLC MediaPlayer implementation. The MediaPlayer works fine for the most part, however upon video conclusion, the MediaPlayer will become unresponsive. From my research, it looks like this could be because the Media is getting unset upon MediaPlayer.Event.EndReached firing (vajehu).

I've been keeping an eye on MediaPlayer.getPlaybackState() and can see that the MediaPlayer object is sitting in the "Ended" state when playback concludes, as expected.

I can go ahead and release my MediaPlayer and re-create it when MediaPlayer.Event.EndReached is fired, but am unsure if this is a good course of action. I am hoping to have the MediaPlayer move back to the beginning of the video and await user input to commence playback again.

(In case it's pertinent - I'm utilising MrMaffen's vlc-android-sdk).


Solution

  • I've since discovered a neat and tidy (and more importantly efficient!) solution for this;

    Upon MediaPlayer.Event.EndReached firing I:

    • Call MediaPlayer.setMedia(media) to reload the Media object
    • Reset a few UI elements relating to my MediaPlayer
    • Finally I set the MediaPlayer position to the start of the Media object with MediaPlayer.setTime(0)

    Side note: since LibVLC's MediaPlayer.setTime(Long position) method doesn't have an effect unless the MediaPlayer.isPlaying(), I needed to write a small wrapper method to asynchronously:

    • MediaPlayer.play() and wait for MediaPlayer.isPlaying()
    • Then MediaPlayer.setTime(0)
    • Finally MediaPlayer.pause()

    A much simpler solution than I expected, though I hope this helps anyone who might be scratching their head whilst working on the same type of project.