How to create LibVLCSharp custom playback controls in Xamarin Forms?

可紊 提交于 2021-01-07 06:43:26

问题


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 ^_^


回答1:


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://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/styles/xaml/inheritance




回答2:


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.



来源:https://stackoverflow.com/questions/61197342/how-to-create-libvlcsharp-custom-playback-controls-in-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!