Search code examples
c#wpfmediaelement

WPF - MediaElement Appearing as Null


I am writing a small application to play/pause and stop a video in WPF.

All is great, but when I want to add manual controls and retrieve the video, it returns a null value, when the video is clearly there and it is not null; or it should not, anyway!

The code for the xaml file is:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="600px" Width="800px">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <MediaElement
            x:Name="mediaElement"
            
            Grid.ColumnSpan="3"
            Grid.Row="0"
            Height="400px"
            LoadedBehavior="Manual"
            Source="C:\Users\User\Videos\the_video_00.mp4"
            UnloadedBehavior="Stop"
            Width="700px"
        />
        <Separator
            Background="Transparent"
            Grid.ColumnSpan="3"
            Grid.Row="1"
            Height="10px"
        />
        <Button
            x:Name="buttonPlay"
            
            Grid.Column="0"
            Grid.Row="2"
            HorizontalAlignment="Center"
            Height="30px"
            Width="75px"
            Click="PlayButton_Click"
        >Play</Button>
        <Separator
            Background="Transparent"
            Grid.Column="1"
            Grid.Row="2"
            Width="490px"
        />
        <Button
            x:Name="buttonStop"

            Grid.Column="2"
            Grid.Row="2"
            HorizontalAlignment="Center"
            Height="30px"
            Width="75px"
            Click="StopButton_Click"
        >Stop</Button>
    </Grid>
</Window>

The code for the C# file is:

using System.Numerics;
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 WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        // ---------------------------------------------------------------------
        // Variables
        // ---------------------------------------------------------------------

        private bool is_playing = false;

        // ---------------------------------------------------------------------
        // Main Window
        // ---------------------------------------------------------------------

        public MainWindow()
        {
            InitializeComponent();
        }

        // ---------------------------------------------------------------------
        // Event Listeneres
        // ---------------------------------------------------------------------

        private void PlayButton_Click(object sender, RoutedEventArgs e)
        {
            // Get the video player.
            var player = this.FindName("mediaElement") as MediaElement;

            // No video player found.
            if (player == null) { MessageBox.Show("Player is null!"); return; }
            

            // Play/Pause the video.
            is_playing = !is_playing;

            if (is_playing)
            {   
                player.Play();
                return;
            }

            player.Pause();
        }

        private void StopButton_Click(object sender, RoutedEventArgs e)
        {
            // Get the video player.
            var player = this.FindName("mediaElement") as MediaPlayer;

            // No video player found.
            if (player == null) return;

            // Play/Pause the video.
            is_playing = false;
            player.Close();
        }
    }
}

The issue is that the name the "mediaElement" is not found and, thus, the video cannot be controlled. I am extremely sure that the video does exists, since when setting the attribute LoadedBehavior="Play", it does play!

Question

How do I get this to work?

For some weird reason it worked before, now it is not!

Thanks!


Solution

  • In StopButton_Click there is as MediaPlayer. Change it to as MediaElement like in PlayButton_Click.

    It is however pointless to use FindName at all, because when you assign x:Name="mediaElement", that will create a field mediaElement in the MainWindow class which you could use directly:

    private void PlayButton_Click(object sender, RoutedEventArgs e)
    {
        is_playing = !is_playing;
    
        if (is_playing)
        {   
            mediaElement.Play();
        }
        else
        {
            mediaElement.Pause();
        }
    }
    
    private void StopButton_Click(object sender, RoutedEventArgs e)
    {
        is_playing = false;
        mediaElement.Close();
    }