从嵌入资源播放视频文件时出错 [英] Error while playing the video file from embedded resource
问题描述
我正在开发用于播放视频文件的 vb.net Windows 应用程序.
I am developing vb.net Windows application for playing the video file.
我以这种方式在嵌入资源中添加了一个视频文件:
I have added a video file in embedded resource this way:
项目->属性.然后选择资源"选项卡.下一步选择添加Rersource"->"来自现有文件".
Project->Properties. Then select the "Resources" tab. Next Select "Add Rersource"->"From Existing File".
我正在尝试播放该文件,但在线上出现运行时错误
I am trying to play the file, buts its giving run time error on the line
Dim myByte As Byte = myStream.ReadByte
错误:未将对象引用设置为对象的实例.
Error : Object reference not set to an instance of an object.
这是代码...
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
我是否遗漏了任何步骤?
am I missing any step ?
推荐答案
实际上您正在使用 My.Resources
.所以你不需要从程序集中读取资源.
In fact you are using My.Resources
. So you don't need to read the resource from assembly.
您可以通过这种方式简单地阅读和使用它:
You can simply read and use it this way:
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 部分是检查文件是否存在并且之前解压过,所以我们不需要再次解压.
The if part is to check if the file exists and extracted before so we don't need to extract it again.
这篇关于从嵌入资源播放视频文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!