Xamarin.Forms: LibVLCSharp lag increases over time
Posted: 28 Oct 2020 03:55
I already posted this question in the Xamarin forum but no one has answered so far. I thought this forum might be more helpful...
I am using the LibVLCSharp library in Xamarin.Forms to stream a jpg video from an IP camera. It all works fine, excepts that when I start the application the actual view of the stream lags about 1 second from real-time and then the difference slowly increases until it is in the order of minutes, i.e. I am watching the past.
It makes me think that the stream is being buffered and then displayed. Is there an option to avoid the buffering, just dropping unused frames and keep in synch with reality?
My current code is as follows. The view:
The code behind:
The model:
I am using the LibVLCSharp library in Xamarin.Forms to stream a jpg video from an IP camera. It all works fine, excepts that when I start the application the actual view of the stream lags about 1 second from real-time and then the difference slowly increases until it is in the order of minutes, i.e. I am watching the past.
It makes me think that the stream is being buffered and then displayed. Is there an option to avoid the buffering, just dropping unused frames and keep in synch with reality?
My current code is as follows. The view:
Code: Select all
<shared:VideoView MediaPlayer="{Binding MediaPlayer}" MediaPlayerChanged="VideoView_MediaPlayerChanged" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
Code: Select all
private void VideoView_MediaPlayerChanged(object sender, MediaPlayerChangedEventArgs e)
{
((ParametersModel)BindingContext).OnVideoViewInitialized();
}
Code: Select all
private bool IsLoaded { get; set; }
private bool IsVideoViewInitialized { get; set; }
private LibVLC LibVLC { get; set; }
private MediaPlayer _mediaPlayer;
public MediaPlayer MediaPlayer
{
get => _mediaPlayer;
set
{
_mediaPlayer = value;
OnPropertyChanged();
}
}
private void Initialize()
{
Core.Initialize();
LibVLC = new LibVLC();
var media = new Media(LibVLC, new Uri("http://192.168.4.1/"));
MediaPlayer = new MediaPlayer(LibVLC) { Media = media };
media.Dispose();
}
public void OnAppearing()
{
IsLoaded = true;
Play();
}
internal void OnDisappearing()
{
MediaPlayer.Dispose();
LibVLC.Dispose();
}
public void OnVideoViewInitialized()
{
BT_Status = "Now initialized...";
IsVideoViewInitialized = true;
Play();
}
private void Play()
{
if (IsLoaded && IsVideoViewInitialized)
{
BT_Status = "Now playing...";
try { MediaPlayer.Play(); }
catch (Exception ex) { BT_Status = ex.ToString(); }
}
}