MediaPlayerElement gets laggy when leaving page and return
Summary
I embedded the MediaPlayerElement
in my shared project like shown below.
<vlc:MediaPlayerElement
MediaPlayer="{Binding MediaPlayer}"
BackgroundColor="{DynamicResource Black}"
EnableRendererDiscovery="True"
>
<vlc:MediaPlayerElement.Style>
<Style TargetType="vlc:MediaPlayerElement">
<Setter Property="IsVisible" Value="False"/>
<Style.Triggers>
<MultiTrigger TargetType="vlc:MediaPlayerElement">
<MultiTrigger.Conditions>
<BindingCondition Binding="{Binding WebCamEnabled}" Value="True"/>
<BindingCondition Binding="{Binding ShowImage}" Value="False"/>
</MultiTrigger.Conditions>
<Setter Property="IsVisible" Value="True"/>
</MultiTrigger>
</Style.Triggers>
</Style>
</vlc:MediaPlayerElement.Style>
<vlc:MediaPlayerElement.PlaybackControls>
<vlc:PlaybackControls
IsAudioTracksSelectionButtonVisible="False"
IsClosedCaptionsSelectionButtonVisible="False"
IsAspectRatioButtonVisible="False"
IsCastButtonVisible="False"
IsPlayPauseButtonVisible="False"
IsRewindButtonVisible="False"
IsSeekButtonVisible="False"
IsSeekEnabled="False"
IsSeekBarVisible="False"
/>
</vlc:MediaPlayerElement.PlaybackControls>
</vlc:MediaPlayerElement>
In the ViewModel
behind of this XAML code, I call the Core.Initialize()
function at OnAppearing()
(it's an async Task).
Afterwards I call the following function to create the MediaPlayer
& LibVlc
object binded to the MediaPlayerElement.
private async Task RefreshWebCamViewAction()
{
try
{
if (!WebCamEnabled) return;
IsLoadingWebCamStream = true;
Uri = Server.WebCamUri;
if (!string.IsNullOrEmpty(Uri))
{
try
{
if (LibVLC == null)
{
List<string> options = new List<string>();
options.Add(string.Format("--file-caching={0}", FileCaching));
LibVLC = new LibVLC(options.ToArray());
}
if (MediaPlayer != null && MediaPlayer.IsPlaying)
{
MediaPlayer.Stop();
}
var Media = new Media(LibVLC, Uri, FromType.FromLocation);
Media.AddOption(string.Format(":network-caching={0}", NetworkBufferTime));
Media.AddOption(string.Format(":clock-jitter={0}", 0));
Media.AddOption(string.Format(":clock-synchro={0}", 0));
await Media.Parse(MediaParseOptions.ParseNetwork);
MediaPlayer = new MediaPlayer(Media)
{
EnableHardwareDecoding = true,
NetworkCaching = Convert.ToUInt32(NetworkBufferTime),
FileCaching = Convert.ToUInt32(FileCaching),
};
}
catch (Exception exc)
{
Debug.WriteLine(string.Format("VlcException => {0} ({1})", exc.Message, exc.StackTrace));
}
}
IsLoadingWebCamStream = false;
}
catch (Exception exc)
{
IsLoadingWebCamStream = false;
}
}
Once this is done, I start the video stream at this action.
private async Task StartWebCamStreamAction()
{
try
{
if (MediaPlayer != null && !MediaPlayer.IsPlaying)
{
ThreadPool.QueueUserWorkItem((Object stateInfo) => {
if (MediaPlayer != null && !MediaPlayer.IsPlaying)
MediaPlayer.Play();
});
}
}
catch (Exception exc)
{
}
}
So far so good.
Minimal project and steps to reproduce
With the code above in my MainPage
, I open a new Page using the Shell.
await Shell.Current.GoToAsync(NewPage);
- Embed the MediaPlayerElement as shown above
- Switch to another page using Shell.Current.GoToAsync()
- Close the page and return
- Web stream becomes laggy and slow
What is the current bug behavior?
Now, when I return to the MainPage
the web stream becomes extremly slow and extremely delayed (laggy).
What is the expected correct behavior?
The stream should play the same as had before leaving the page.
Relevant logs and/or screenshots
Environment
- OS: iOS / Android
- Version
- Device: Iphone 11 (real device), Iphone SE 2 (simulator), Android Pixel 3 (emulator)
- VideoLAN.LibVlac.Android 3.2.0
- VideoLAN.LibVlac.iOS 3.3.10
- LibVLCSharpForms version 3.4.7
Possible fixes
Maybe it's just an issue by me. Do I need to do anything at OnDisappearing() like stopping the MediaPlayer or Dispose it? Or does the MediaPlayerElement handle this on its own and automatically restarts the last playing video?
Also, when I recall the RefreshWebCamViewAction & StartWebCamStreamAction the video plays normal again. Does it maybe loose the settings (network-caching, clock-jitter and so on) when a new page is opened?