K
K
Kokos Belyi2021-06-13 18:44:08
WPF
Kokos Belyi, 2021-06-13 18:44:08

WPF How to set play button to specific video?

Hello =)
Can someone tell me how to set the buttons to play different videos in MediaElement? something like this >>>
Thanks in advance)
60c626ab0395d754127454.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2021-06-13
@primatsKompom

On click, change src and start the player like this

using System;
using System.Windows;
using System.Windows.Controls;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            if (sender is Button btn)
            {
                MediaControl.LoadedBehavior = MediaState.Manual;
                MediaControl.Source = new Uri($"video/{btn.Name}.mp4", UriKind.Relative);
                MediaControl.Play();
            }
        }
    }
}

<Grid HorizontalAlignment="Left" VerticalAlignment="Top">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <MediaElement Grid.Row="0" Height="200" Width="300" Name="MediaControl"></MediaElement>
        <Button Grid.Row="1" Click="ButtonBase_OnClick" Name="Btn1">one</Button>
        <Button Grid.Row="2" Click="ButtonBase_OnClick" Name="Btn2">two</Button>
        <Button Grid.Row="3" Click="ButtonBase_OnClick"  Name="Btn3">three</Button>
    </Grid>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question