Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Trouble with MediaElement (setting source, play, stop, etc...)

$
0
0

Hello. I am working on a project involving MediaElements. The MediaElements used to work just fine but they suddenly stopped working. To troubleshoot this, I created a test project to make sure my code related to the MediaElement is correct. Unfortunately, I cannot get even this simple code to work! I'm sure I'm missing something small here.

XAML:

<Window x:Class="WPFMediaElementTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="900" Width="1300"><Grid><MediaElement Name="VideoControl" Grid.Column="0" Grid.Row="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" LoadedBehavior="Manual" MediaEnded="VideoControl_MediaEnded" Visibility="Collapsed"/></Grid></Window>

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WPFMediaElementTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string path = @"c:\FANCY.mp4";
        public MainWindow()
        {
            InitializeComponent();
            this.Show();
            ProcessAndShowContent();
        }
        public void ProcessAndShowContent()
        {
            ShowVideo(path);
        }
        public void ShowVideo(string pathStr)
        {
            VideoControl.Source = new Uri(pathStr);
            VideoControl.Visibility = Visibility.Visible;
            VideoControl.Play();
        }
        public void StopMedia()
        {
            VideoControl.Visibility = Visibility.Collapsed;
        }
        private void VideoControl_MediaEnded(object sender, RoutedEventArgs e)
        {
            StopMedia();
            ProcessAndShowContent();
        }
    }
}

I realize there is a more concise way to accomplish what this code does, but I wanted to replicate the number methods, etc., I use in my other project to make it a more accurate test (the other project does much more in each method so these methods are a skeletal representation of the other methods, designed to test just the pieces related to the MediaElement).

When I run this application, the MainWindow appears but the body of it is just plain white. I don't see the video at all. I can play the video by opening it manually in Windows Media Player so I know it is not a problem with the file. Can someone advise on what I am missing? Thank you for your help.



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>