'How to Intercept errors and change the style of libvlcsharp xamarin forms

I have correctly implemented LibVlcSharp in my Xamarin.Forms project.

using (var _libVLC = new LibVLC())
            {
                var media = new Media(_libVLC, _link, FromType.FromLocation);
                myvideo.MediaPlayer = new MediaPlayer(media)
                {
                    Fullscreen = true
                };
                myvideo.MediaPlayer.Play();
            };

In my video player I stream video from google drive but sometimes it can happen that the media gives error and this error is shown:

enter image description here

Streaming works, there may be many reasons for the error but that's not what interests me. I would like to understand how to intercept this error and customize this error label but I have not found anything in the documentation or online.

I also wanted to know if it was possible to change the style of the buttons, I found this property myvideo.PlaybackControls that has many other properties but I do not know if it is correct and how to apply it.

Thanks



Solution 1:[1]

There is ShowError method but no other details

private void ShowError()
{
    var errorTextBlock = ErrorTextBlock;
    if (errorTextBlock != null)
    {
        errorTextBlock.Text = string.Format(ResourceLoader.GetString("Error"), Manager.Get<StateManager>().MediaResourceLocator);
        VisualStateManager.GoToState(this, ErrorState, true);
    }
}

You can get more log by subscribing to LibVLC.Log event

I havent run code but I believe you can intercept errors with ErrorOccured event as below

var stateManager = Manager.Get<StateManager>();
stateManager.ErrorOccured += (sender, e) => ShowError();

That should give you what is causing the issue, if not you should raise an issue on libvlcsharp github

Looks like they have created PlayBackControls property bind to PlayBackControls of LibVLCSharp.Uno library PlayBackControls element and the icons are applied through this XAML

<ContentPresenter Grid.Row="1" Content="{TemplateBinding PlaybackControls}" />

You should be able to change it by binding as far as it matches PlayBackControls.Android.iOS

Solution 2:[2]

Have a look at

 <Label Text="{TemplateBinding ErrorMessage}" Style="{TemplateBinding MessageStyle}"
                       IsVisible="{TemplateBinding ErrorMessage, Converter={StaticResource ObjectToBoolConverter}}" />

From https://code.videolan.org/videolan/LibVLCSharp/-/blob/3.x/LibVLCSharp.Forms/Shared/Themes/Generic.xaml. This is the base style provided by LibVLCSharp that you want to modify.

You can override the style by following this guide https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/styles/xaml/inheritance

In XAML, that could be

<Style x:Key="labelStyle" TargetType="Label" BasedOn="{StaticResource MessageStyle}">
   <Setter Property="TextColor" Value="Teal" />
</Style>

I created a ticket to add more docs about this https://code.videolan.org/videolan/LibVLCSharp/-/issues/309

Solution 3:[3]

You can hide all items and add your controls.

<vlc:MediaPlayerElement x:Name="VideoView"
                            AbsoluteLayout.LayoutFlags="All"
                            EnableRendererDiscovery="False"
                            AbsoluteLayout.LayoutBounds="0,0,1,1">

        <vlc:MediaPlayerElement.PlaybackControls>
            <vlc:PlaybackControls IsVisible="true"
                                  IsAspectRatioButtonVisible="True"
                                  ShowAndHideAutomatically="True"
                                  IsRewindButtonVisible="True"
                                  IsCastButtonVisible="True"
                                  IsLockButtonVisible="True"
                                  IsSeekBarVisible="True"
                                  IsSeekButtonVisible="True"
                                  IsSeekEnabled="True"
                                  IsTracksButtonVisible="True">

                <vlc:PlaybackControls.RemainingTimeLabelStyle>
                    <Style TargetType="Label">
                        <Setter  Property="IsVisible"
                                 Value="false" />
                    </Style>
                </vlc:PlaybackControls.RemainingTimeLabelStyle>

                <vlc:PlaybackControls.MessageStyle>
                    <Style TargetType="Label">
                        <Setter  Property="IsVisible"
                                 Value="false" />
                        <Setter  Property="TextColor" Value="Transparent"/>
                      
                        
                        <Setter Property="Text"
                                Value="0" />
                    </Style>
                </vlc:PlaybackControls.MessageStyle>


                <vlc:PlaybackControls.ElapsedTimeLabelStyle>
                    <Style TargetType="Label">
                        <Setter  Property="IsVisible"
                                 Value="false" />
                    </Style>
                </vlc:PlaybackControls.ElapsedTimeLabelStyle>
            </vlc:PlaybackControls>
        </vlc:MediaPlayerElement.PlaybackControls>
    </vlc:MediaPlayerElement>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 mfkl
Solution 3 ouflak