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
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?
There is a problem to play a "resource" audio/video file using
MediaElement
. It's seems the media element does not support packed URI likeSource="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 theMediaTimeline
source to make reference to the temporary file.Example:
NOTE: Temporary created file should be deleted when it no longer needed.