question

MojtabaHakim-8125 avatar image
0 Votes"
MojtabaHakim-8125 asked PeterFleischer-3316 commented

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

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 ♥


dotnet-csharpwindows-wpfdotnet-wpf-xaml
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Paul-5034 avatar image
1 Vote"
Paul-5034 answered Paul-5034 commented

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.

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Can you please give me more advice? or some code about this

0 Votes 0 ·
Paul-5034 avatar image Paul-5034 MojtabaHakim-8125 ·

Try changing the Source of your <MediaElement> to start with pack://siteoforigin:

259941-image.png

But generally you can also add a MediaFailed event handler to be alerted if your media fails for any reason. If you click on your <MediaElement> in Design view, click the events button (lightning bolt above the Properties panel), then double-click the "MediaFailed" event type:

259860-image.png

Then you can add an event handler to Debug.WriteLine the error message if it occurs:

private void TestVideo_MediaFailed_1(object sender, ExceptionRoutedEventArgs e) {
    var details = $"Failed to load media: {e.ErrorException.Message}";

    Debug.WriteLine(details);
    MessageBox.Show(details);
}


This can give you an idea of whether your <MediaElement> failed to load.


1 Vote 1 ·
image.png (5.5 KiB)
image.png (133.7 KiB)

I have updated my question in stackoverflow please check it out , I followed your steps and it shows me this :259983-screenshot-54.png


 pack://siteoforigin:,,,/Medias/CorrectVideo.mp4


0 Votes 0 ·
screenshot-54.png (23.9 KiB)
Show more comments
PeterFleischer-3316 avatar image
1 Vote"
PeterFleischer-3316 answered PeterFleischer-3316 commented

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

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

it won't be embed !

0 Votes 0 ·

Hi,
my test:

 <Window x:Class="WpfApp1.Window027"
         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="MojtabaHakim-8125_221113" Height="450" Width="800">
   <Grid>
     <MediaElement x:Name="me" Stretch="Fill" Source="pack://siteoforigin:,,,/Medias/Video1.mp4"/>
   </Grid>
 </Window>

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

0 Votes 0 ·