Video and music not appearing in WPF when used as an asset

435 views Asked by At

I'm now entering WPF and I had a question, although it seems simple, I'm still learning

I am creating an application that shows a video and a song when it opens.

If I define the full path like:

C:\myapp\media.mp4

Using this path in Source works normally

the problem starts when I add the video and music as a resource, it stops working

enter image description here

enter image description here

I am using the following code:

<Grid>
<StackPanel Height="768" VerticalAlignment="Bottom" Width="1024" HorizontalAlignment="Left">
    <MediaElement x:Name="Mymedia" VerticalAlignment="Top" Width="1024" Height="768">
        <MediaElement.Triggers>
            <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                <BeginStoryboard>
                    <Storyboard>
                        <MediaTimeline Source="Assets/media.mp4" Storyboard.TargetName="Mymedia"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </MediaElement.Triggers>
         <MediaElement.Triggers>
            <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                <BeginStoryboard>
                    <Storyboard>
                        <MediaTimeline Source="Assets/isurge.mp3" Storyboard.TargetName="Mymedia"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </MediaElement.Triggers>
    </MediaElement>
</StackPanel>

using resource the video does not run can someone tell me why?

2

There are 2 answers

4
Jackdaw On BEST ANSWER

There is a problem to play a "resource" audio/video file using MediaElement. It's seems the media element does not support packed URI like Source="pack://application:,,,/Assets/media.mp4".

Similar issue is described on the Microsoft forum:

Using Relative File Path in MediaTimeline

How to play a "resource" audio file using MediaElement?

But it is possible to obtain audio/video from the resources and set the MediaTimeline source programmatically. The idea is extract audio/media content from a resources and write it to the temporary file. Then set URI for the MediaTimeline source to make reference to the temporary file.

Example:

Set a file Build Action to Embedded Resource.

In the code:

  var resourceName = "YourAssemblyName.Assets.isurge.mp3";
  using (var fstream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
  {
      var ext = resourceName.Substring(resourceName.LastIndexOf("."));
      var pathfile = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ext;
      using (FileStream outputFileStream = new FileStream(pathfile, FileMode.Create))
      {
          fstream.CopyTo(outputFileStream);
      }
      mt.Source = new Uri(pathfile, UriKind.RelativeOrAbsolute);
  }

Set the MediaTimeline name:

 <MediaTimeline x:Name="mt" Storyboard.TargetName="audio" RepeatBehavior="Forever"/>

NOTE: Temporary created file should be deleted when it no longer needed.

1
Kodaloid On

This looks like you may just not have the correct path. Try using something like the following:

<MediaTimeline Source="pack://application:,,,/Assets/media.mp4" Storyboard.TargetName="Mymedia"/>

This works for me when building .NET Core WPF apps, other versions of .NET may vary.