How to display a video in the window from the path embedded in the program in C# WPF?

Mojtaba_Hakim 281 Reputation points
2022-11-13T19:41:56.12+00:00

In the C# program, I want to display and play a video from within my program in a window.

The path that I give to my video source as an internal path of the embed type works in design mode, but it does not display the video when running the program, but if I give it the direct address of the file in Windows, it works.

This is despite the fact that if I want to show a photo or gif from the same path, it works and shows well

Full description plus code and image :
how-to-show-an-embedded-video-inside-of-a-window-in-c-sharp-wpf-application

Please guide me
, thanks for taking your time ♥

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,673 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,260 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
765 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. P a u l 10,406 Reputation points
    2022-11-13T20:33:11.103+00:00

    You could try pack://siteoforigin instead of pack://application in your Source path. Btw I found this out because there's a MediaFailed event that you can add a listener to on your MediaElement which will tell you the problem.

    MediaElement silently fails when loading its source so it's best to listen for that error & at least log it to your debug console.

    1 person found this answer helpful.

  2. Peter Fleischer (former MVP) 19,231 Reputation points
    2022-11-14T06:54:16.223+00:00

    Hi,
    please, use correct Uri (without slash):

     <MediaElement Stretch="Fill" Source="Medias/Video1.mp4" />  
    

    or

      <MediaElement Stretch="Fill" Source="pack://siteoforigin:,,,/Medias/Video1.mp4"/>  
      
    

    folder "Medias" in your project;
    file "Video1.mp4" in this folder;
    properties: Content and Copy always or if newer

    1 person found this answer helpful.