Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play embedded video resource as stream

EDIT: I changed my question to better clarify the issue. How is it possible to play a video from a byte array (taken from embedded resource) using DirectShow.Net library?

Since I'm going to prevent users from accessing the video file, I need to embed the video file as resource and play it.

Thanks in advance.

like image 612
Harry.B Avatar asked Jul 03 '11 16:07

Harry.B


2 Answers

It's a bit non-standard, but you could use something like WCF to self-host an endpoint inside your desktop application. Then set the source of the video input to be the "URL" to your self-hosted endpoint. That would work for WPF or WinForms. Not sure about Silverlight though.

The self-hosted endpoint could pull the media from your embedded resources and stream it from there.

like image 157
NathanAW Avatar answered Sep 21 '22 15:09

NathanAW


It sounds to me like the problem is not so much how to use the DirectShow library (the `DirectShow.Net Forum is specifically designed for that), but rather how to use an embedded resource.

I ran into something similar a few years back on a contract job where an employer was worried that some customer might steal his proprietary information. My information was in hundreds of PDF documents, but the idea works the same for video files.

Here's how I tackled the problem:

  • First, place the video file in your list of resources: I use Visual Studio, so I go to the Project's Properties, click the Resources tab, select the Files option, then select Add Resource > Add Existing File...

  • Add the following two namespaces to the code file you will be using:

using System.IO;
using System.Diagnostics;
  • Finally, where you want to play your video file, just do something similar to the following:
 Process player = null;
 string tempFile = "~clip000.dat";
 try {
   File.WriteAllBytes(tempFile, Properties.Resources.MyMovie_AVI);
   player = Process.Start(tempFile);
   player.WaitForExit();
 } finally {
   File.Delete(tempFile);
 }

Most likely, you will not be calling the Process.Start method, but rather the appropriate DirectShow method. The idea is still the same: Extract your resources as a byte array, write them to a new, temporary file, use the file, then delete that file whenever you are done.

Be sure to put the Delete statement in the finally block so that if any errors occur or your user closes the program while the file is still playing, your application still cleans up the old file.

EDIT:

I think this might be a viable way of doing this:

using (MemoryStream ms = new MemoryStream(Properties.Resources.MyMovie_AVI)) {
  // Now you have to find a way in `DirectShow` to use a Stream
}
like image 36
jp2code Avatar answered Sep 21 '22 15:09

jp2code