0

I've been searching for days now for a guide on how to create the custom playback controls for LibVLCSharp that everyone seems to talk about, which I never found a guid for.

I simply want to create other buttons with event handlers for the bottom playback control panel, I tried this but throws a System.NullReferenceException exception on startup while getting into break mode...

<vlc:MediaPlayerElement MediaPlayer="{Binding MediaPlayer}" LibVLC="{Binding LibVLC}">
    <vlc:MediaPlayerElement.PlaybackControls>
        <vlc:PlaybackControls>
            <vlc:PlaybackControls.ControlTemplate>
                <ControlTemplate>
                    <Grid>
                        <StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
                            <Button Grid.Column="0" Text="Test 1"/>
                            <Button Grid.Column="1" Text="Test 1"/>
                            <Button Grid.Column="2" Text="Test 1"/>
                        </StackLayout>
                    </Grid>
                </ControlTemplate>
            </vlc:PlaybackControls.ControlTemplate>
        </vlc:PlaybackControls>
    </vlc:MediaPlayerElement.PlaybackControls>
</vlc:MediaPlayerElement>

I want it to act just like the original one (Auto hides, overlays on tapping, etc...) but with my own layout and controls. I also thought about using the existing one and try to override their handler to implement my own code and override the text property for each button to change its icon but no luck of finding any help.

Thanks in advance ^_^

Mohamed Ashraf
  • 181
  • 3
  • 19

2 Answers2

5

The code you are interested in is here: https://code.videolan.org/videolan/LibVLCSharp/-/blob/3.x/src/LibVLCSharp.Forms/Shared/Themes/Generic.xaml

I also thought about using the existing one and try to override their handler to implement my own code and override the text property for each button to change its icon

That'd be the way to go.

This previous SO question might answer your question: https://stackoverflow.com/a/14217500/4064749

Just create a new Style based on PlaybackControlsStyle, override what you want and then set it on the PlaybackControls element.

I created https://code.videolan.org/videolan/LibVLCSharp/-/issues/309 recently to track the need of a tutorial to customize the MediaElement.

Further docs on style inheritance: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/styles/xaml/inheritance

mfkl
  • 1,914
  • 1
  • 11
  • 21
  • I don't really understand what you've done, I tried to create a quick thing style with target type `TemplatedView` where its control template property is set to an empty stacklayout with red background (Just testing with dummy elements). I set the playbackcontrols style to the style key and nothing worked, the old playback controls is showing up like if I did nothing... What am I missing ?! – Mohamed Ashraf Apr 14 '20 at 12:47
  • Same exception occurs also if I changed the style name to anything else but the one you provided. I just want to explain that I want to create the whole playbackcontrols panel contents and I think it's possible as the documentation mentions that the whole playbackcontrols is customizable ... – Mohamed Ashraf Apr 14 '20 at 13:09
1

I finally found the problem which was making exceptions, when I create custom control template which completely correct, the MediaPlayerElement code behind by LibVLCSharp developers itself cannot find the elements with the names defined anymore as they used hardcoded names for the buttons and views instead of using bindings and dynamic setters.

Thus, several workarounds could be made to fix such issue, here are some of my ideas:

  1. Use the generic style documented here and modify it without removing any elements but rather hide them out or overlay them.
  2. Create your own style with controls obtaining the same names of the original ones.
  3. Find a way to modify or maybe create a whole new playback control element using the original one which can be found here and here.

Thanks to mfkl's answer which helped me find out how everything worked under the hood to come up with the explaination, even though this took me a couple of days to figure out.

Mohamed Ashraf
  • 181
  • 3
  • 19