Search code examples
c#videoresourceswmp

C# - Play videos from resources in wmp component


I have a wmp component in a C# Windows Forms and i want it to play a video (.avi) from the solution's resources. I need to know the code for the wmp component to find the video. Suggestions?


Solution

  • Currently there is a way over streaming the file.

    First of all, we need a place where it should be always possible

            string streamPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\";
    

    Next Step an Instance of the MediaPlayer

        WindowsMediaPlayer wmp = new WindowsMediaPlayer();
    

    Then we need to stream the Assembly Resource

        Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Smartis.Resources.Natur.wmv");
    
        using (Stream output = new FileStream (streamPath + "mediafile.avi", FileMode.Create))
        {
            byte[] buffer = new byte[32*1024];
            int read;
    
            while ( (read= stream.Read(buffer, 0, buffer.Length)) > 0)
            {
                output.Write(buffer, 0, read);
            }
        }
    

    Finally we should be able to load the file.

        wmp.URL = streamPath + "mediafile.avi";
        wmp.controls.play();
    

    After playing don't forget to clear the folder:

        File.Delete(streamPath + "mediafile.avi");