1

I put my files in the resource list

I use visual studio so I went to the Project Property > Resource Tab > Select file options> Add resource> Add Existing File ...

I want to keep my files private so I thought of using something like

string temp = "~tempv.dat";
File.WriteAllBytes(temp, Properties.Resources.MyVideo);

Now how can I do the following with my tempv.dat file

  • How can I reproduce it?
  • How can I put it in the temporary windows folder
  • How can i remove it when closing wpf

My Xaml Code:

<MediaElement Name="myVid" VerticalAlignment="Stretch" UnloadedBehavior="Stop" LoadedBehavior="Manual" />
  • It should be possible to obtain [resource as a stream](https://stackoverflow.com/q/1388052/1997232) instead of using files. Not sure if `MediaElement` can play stream, in worst case you have to use some library. – Sinatr Nov 24 '20 at 15:12

1 Answers1

0

Include your media file to project as embedded resources. This is easier because of it is necessary to know the file extension when saving to disk. The MediaTimeline elements requires the file extension, does not support a stream. Then 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.

Code behind:

 private string MediaFilePathName { get; set; }

 var resourceName = "[YourAssemblyName][.Folder].Movie.mp4"; // NOTE: Case sensitive
 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);
      }
      media.Source = new Uri(pathfile, UriKind.RelativeOrAbsolute);
      MediaFilePathName = pathfile;
  }

In the XAML:

<Window ...  Closed="Window_Closed">
    <Grid>
        <MediaElement x:Name="audio">
            <MediaElement.Triggers>
                <EventTrigger RoutedEvent="MediaElement.Loaded">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <MediaTimeline x:Name="media" RepeatBehavior="Forever"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </MediaElement.Triggers>
        </MediaElement>
    </Grid>
</Window>

The temporary file should be deleted when it no longer needed:

private void Window_Closed(object sender, EventArgs e)
{
    if (File.Exists(MediaFilePathName))
    {
        File.Delete(MediaFilePathName);
    }
}

Another way to use third party library that will execute all the steps.

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
  • The code works fine, but if the user is in the temporary folder he will find the media with a name similar to `325ae577-9f90-4c6f-9c73-2009a301168c.mp4` Is there any way I can leave this media in a .dat format? – pablo kentch Nov 24 '20 at 16:16
  • @pablo kentch: Unfortunately no. It looks like `MediaElement` it uses file extensions to determine which filter is needed to play it. But it is possible, for example, to use `Windows.Closed` event to remove it. I have updated the code above. – Jackdaw Nov 24 '20 at 16:24
  • @pablo kentch: Perhaps you should check possibility to save files under [Isolated storage](https://learn.microsoft.com/en-us/dotnet/standard/io/isolated-storage). Using isolated storage enables to store data in a manner that is controlled by the computer's security policy. – Jackdaw Nov 24 '20 at 16:39
  • understand it is a pity not to accept other formats, Thanks for the beautiful explanation Jackdaw – pablo kentch Nov 24 '20 at 16:40
  • @pablo kentch: Actually I haven't used, just read it. You can look at it in the documentation: [https://learn.microsoft.com/en-us/dotnet/standard/io/isolated-storage](https://learn.microsoft.com/en-us/dotnet/standard/io/isolated-storage) – Jackdaw Nov 24 '20 at 16:50
  • Thank you very much – pablo kentch Nov 24 '20 at 17:03