2

I have libvlcsharp working for both my windows and android project. Its very nice and easy to use! But I can't find any way to make the playback automatically loop

I've tried using the mediaplayer event handlers to restart playback and passing options into the media after creation, but nothing seems to work.

I tried:

loopSong = new Media(VLCMediaPlayer.LibVLC, xmlLoader.GetResourceStream(loopFileName));
loopSong.AddOption(":no-video");
loopSong.AddOption(":input-repeat");

and

static VLCMediaPlayer()
{
    Core.Initialize();
    LibVLC = new LibVLC();
    MediaPlayer = new MediaPlayer(LibVLC);
    MediaPlayer.EndReached += MediaPlayer_EndReached;
}

private static void MediaPlayer_EndReached(object sender, EventArgs e)
{
    MediaPlayer.Stop();
    MediaPlayer.Play();
}

Playback simply ceases. The events are called but MediaPlayer.WillPlay is false and calling Play does nothing.

Ben W
  • 23
  • 1
  • 3

2 Answers2

6

This is a famously known bug (by us at least) that happens when you call libvlc from a libvlc callback. Since Libvlc calls events from the thread that plays the video, when you call Stop(), you're waiting synchronously for that thread to finish, causing the deadlock.

Here is the workaround I proposed for Vlc.DotNet : https://github.com/ZeBobo5/Vlc.DotNet/wiki/Vlc.DotNet-freezes-(don't-call-Vlc.DotNet-from-a-Vlc.DotNet-callback)

public void MediaEndReached(object sender, EventArgs args)
{
   ThreadPool.QueueUserWorkItem(() => this.MediaPlayer.Stop());
}

This will probably be solved in a future version of LibVLCSharp, see https://code.videolan.org/videolan/LibVLCSharp/issues/122

cube45
  • 3,429
  • 2
  • 24
  • 35
  • Thanks, I'll give it a shot soon! There's no built in looping feature? – Ben W Jun 07 '19 at 10:27
  • As far as I know, there's no currently no way to do that. There might be something in the media list API, but it's already deprecated. The libvlc's media player plays one and only one media. – cube45 Jun 07 '19 at 14:02
  • The threadpool thing works, BUT there's a noticeable gap in the playback loop. I am going to experiment with having two media players to see if I can make a seamless playback loop – Ben W Jun 07 '19 at 14:22
  • gapless playback is planned for libvlc 4 if I remember correctly. In the meantime, try to play with two media and preparse the second one while the first is playing – cube45 Jun 07 '19 at 14:24
  • I am new to libvlc so I'm not sure what you mean by preparse. You mean load the media into memory before its time to play it? – Ben W Jun 07 '19 at 14:38
  • I mean something like `secondMedia.Parse(...)` and when the video ends, do `mediaPlayer.Play(secondMedia)` – cube45 Jun 07 '19 at 15:01
  • There is still a gap even with the parse. Its hard to say if the gap comes from a delay in EndReached or if its a delay in starting. I see this related question: https://stackoverflow.com/questions/25648337/using-vlc-to-host-a-stream-of-an-infinite-video-loop is there anyway to pass these parameters in and make them work? – Ben W Jun 08 '19 at 00:53
  • You can pass `--loop` in the LibVLC constructor, as mtz suggested. I'm not sure it's still supported though. – cube45 Jun 12 '19 at 13:00
5

This should achieve what you're looking for

new LibVLC("--input-repeat=2")

Playlist APIs will be easier to use in LibVLC 4.

mfkl
  • 1,914
  • 1
  • 11
  • 21
  • This is cleaner than above, but its still not a perfect loop. Interestingly, it actually restarts the playback a split second before the song actually ends – Ben W Jun 08 '19 at 13:46
  • As cube45 said, gapless playback will be possible from libvlc 4. – mfkl Jun 10 '19 at 02:31