如何从资源加载视频到winforms?

时间:2014-02-04 20:06:58

标签: c# resources mp4

我想在我的Windows窗体应用程序中添加视频文件,视频格式类型为MP4。我已经搜索了如何添加视频,我找到了使用Windows媒体播放器库的答案。

我使用过它,但是一旦我输入了我的文件的完整URL,它就可以在我的机器上运行,但是当我将它安装在任何其他机器上时,它就无法播放。我知道我提供视频位置的原因是我的HD位置,然后我进一步搜索并找到嵌入资源,我确实将构建操作更改为嵌入资源,但同样有问题。

如何在我的代码中访问我的嵌入文件?我已尝试过类似namespace.file.mp4namespace.properties.resources.file.mp4的方式,但它说:

  

错误1无法将类型'byte []'隐式转换为'string'L:\ Project SciMult-calc \ videotesting2 \ videotesting2 \ Form1.cs 29 41 videotesting2

2 个答案:

答案 0 :(得分:1)

当您访问资源时,它是byte [],这就是您无法播放视频的原因。 你的方法想要一个不是byte []的路径。

您可以将资源写入磁盘然后播放。

需要

名称空间

using System.Reflection;
using System.IO;

像这样的东西

//Place it in the directory of your application
string mp4Path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "mp4File.mp4");

//check if it hasn't been written to disk yet
if (!File.Exists(mp4Path))
{
    //write it to disk
    File.WriteAllBytes(mp4Path, namespace.properties.resources.file.mp4);
}

//play using mp4Path

答案 1 :(得分:0)

这是我的简短代码,我想在用户点击"关于" menustripitem

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Resources;

namespace videotesting2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.URL = @"L:\about.mp4";

        }
    }

}