C# - Play videos from resources in wmp component

最后都变了- 提交于 2019-12-25 15:54:12

问题


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?


回答1:


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");


来源:https://stackoverflow.com/questions/17410199/c-sharp-play-videos-from-resources-in-wmp-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!