0
votes

I want to display video. I want the video will play with a click on button. The video does not play. I put the video into the project.

I want that the video source will be YouTube.

My XAML code is:

<Window x:Class="MediaElementApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="467.91" Width="1300">
<Grid>
    <MediaElement x:Name="mediaElement" HorizontalAlignment="Left" Height="418" Margin="246,10,0,0" VerticalAlignment="Top" Width="1036" LoadedBehavior="Manual" UnloadedBehavior="Stop" Source="Images\Wildlife.wmv" />
    <Button x:Name="play" HorizontalAlignment="Left" Margin="538,161,0,0" VerticalAlignment="Top" Width="100" Height="84" Click="play_Click" >
        <Button.Background>
            <ImageBrush ImageSource="Images/smiley.jpg"/>
        </Button.Background>
    </Button>

</Grid>

c# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

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

        private void play_Click(object sender, RoutedEventArgs e)
        {

            mediaElement.Play();
        }
    }
}

I would like for help.

2

2 Answers

1
votes

the media source should be from your file system example

mediaElement.Source = new Uri(@"C:\User\Admin\Images\Wildlife.wmv")
0
votes

Yes, Joe B is right. Another approach is to use the code behind method indicated below but this demands to place the media file in a subfolder within the application.

var videoPath = Directory.GetCurrentDirectory();
mediaElement.Source = new Uri(videoPath + @"\Images\Wildlife.wmv", UriKind.Relative);
mediaElement.Play();