【问题标题】:Error while playing the video file from embedded resource从嵌入资源播放视频文件时出错
【发布时间】:2016-01-08 15:06:48
【问题描述】:

我正在开发用于播放视频文件的 vb.net Windows 应用程序。

我以这种方式在嵌入式资源中添加了一个视频文件:

项目->属性。然后选择“资源”选项卡。下一步选择“添加 重新来源”->“来自现有文件”。

我正在尝试播放该文件,但它在行上给出了运行时错误

 Dim myByte As Byte = myStream.ReadByte

错误:对象引用未设置为对象的实例。

这里是代码...

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

   Dim aPath As String = Path.GetDirectoryName(Assembly.GetExecutingAssembly.GetModules()(0).FullyQualifiedName)
    Dim myStream As Stream
    myStream = Assembly.GetExecutingAssembly.GetManifestResourceStream("111.mp4")
    Dim myFileStream As New FileStream("111.mp4", FileMode.Create)
    Dim myFileBinary As New BinaryWriter(myFileStream)
    Try
        Dim myByte As Byte = myStream.ReadByte
        While Not myByte = -1
            myFileBinary.Write(myByte)
            myByte = myStream.ReadByte
        End While
    Catch ex As Exception
    Finally
        myFileStream.Close()
    End Try

    AxWindowsMediaPlayer1.URL = Path.Combine(aPath, "111.mp4")
    AxWindowsMediaPlayer1.settings.autoStart = True


End Sub

我是否缺少任何步骤?

【问题讨论】:

  • 似乎没有可获取的具有该名称的清单资源,因此GetManifestResourceStream 的结果是Nothing。是否有任何特殊原因使您不只是通过项目属性的资源页面添加资源,然后通过My.Resources 获取它?
  • 实际上您正在使用 My.Resources。所以你不需要从程序集中读取资源。您的文件存储在My.resources 的字节数组属性中,您可以使用File.WriteAllBytes 简单地保存资源并像我在答案中那样使用它。

标签: vb.net winforms


【解决方案1】:

事实上你正在使用My.Resources。所以你不需要从程序集中读取资源。

您可以通过这种方式简单地阅读和使用它:

Dim FilePath = Path.Combine(Application.StartupPath, "video.wmv")
If (Not File.Exists(FilePath)) Then
    File.WriteAllBytes(FilePath, My.Resources.video)
End If

AxWindowsMediaPlayer1.URL = FilePath
AxWindowsMediaPlayer1.Ctlcontrols.play()

if部分是检查文件是否存在并在之前提取过,所以我们不需要再次提取它。

【讨论】:

  • 我错过了您所说的以这种方式添加文件的部分问题:Project->Properties. Then select the "Resources" tab. Next Select "Add Rersource"->"From Existing File". 我编辑了问题以使其更具可读性。我还编辑了答案,所以虽然原始答案在您使用嵌入式资源时非常有用,但忽略以前的讨论,只应用上面的答案:)
  • 感谢您的努力...现在工作正常...如果您在实施上述事情时出现的小问题给我建议会很好...stackoverflow.com/questions/34738401/…
  • 不客气 :) 我会看看这个问题 :)
  • 这个问题有答案。你可以使用它:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多