使用此 DirectX 视频播放器播放您的 AVI 文件
使用 Microsoft 的 DirectX.AudioVideoPlayback 播放 AVI 文件。
注意:唯一正在更新的版本是 Visual Studio 2012
版本。对于那些以为下载的是视频播放器却收到了 MP3 播放器的人,哦,抱歉。rspPlayer 并不是您所想的那样。请下载中间的链接以获取视频播放器。再说一次……抱歉!
2012 版本简介
2012 版本看起来类似于 VLC 媒体播放器。您可以从 30 秒到 5 分钟进行快进和快退。它具有全屏功能,并在全屏时显示返回正常视图的消息。它工作得非常好。
工作所需!
您需要适用于 .Net 3.5 的最新 DirectX SDK。Visual Studio 2012,VB.NET。该播放器使用 .NET Framework 3.5 以兼容 Microsoft.DirectX.AudioVideoPlayBack.dll 库。2012 版本现在可以播放 *.avi, *.flv, *.wmv, *.divX, *.xvid, *.mpeg, *.mpg 文件,只要您安装了相应的编解码器。
引言和背景
几个月前,我开始涉足 DirectX 以播放 AVI 文件,在搜索过程中,我偶然发现了 Mike Gold 的 C# 版本,可以在 这里找到。它为您的进一步开发提供了一个极好的起点。我将他的代码转换成了 VB,这花费了我不少时间。我本可以使用转换程序或使用提供此功能的网站,但我决定自己动手。这是一种很好的练习。
经过几次构建和几次更新,我推出了 2012 版本。我使用了一个面板来全屏显示视频。当您处于全屏模式时,它并不是真正的全屏模式。我将面板的高度和宽度设置为 Screen.PrimaryScreen.WorkingArea.Height
和 Screen.PrimaryScreen.WorkingArea.Width
,并将工具栏(顶部和底部)的可见属性设置为“False
”。这模仿了全屏模式。我添加了一个标签,向用户显示退出全屏模式的热键。热键标签会以 2 分钟、12 分钟和 25 分钟的间隔通过计时器显示。通过右键单击快进和快退按钮,会出现一个上下文菜单,您可以向前或向后跳过 30 秒到 5 分钟。我删除了音量和平衡滑块。我用几个新标签替换了标签,以便仅在进度条之前和之后(在上面的图片中)显示完整时长和当前时间。我保留了进度条来处理当前的视频位置。这仅适用于 VS 2012 版本。
通过菜单栏添加了保存和打开已保存的子程序
添加了 mnuFSave
和 mnuFOpenSavedFile
菜单点击事件,以防您需要匆忙离开。您只需按暂停、按保存、按停止并关闭应用程序即可。当您返回应用程序时,可以通过按 mnuFOpenSavedFile
菜单项来从您离开的地方继续播放。这是我用来执行此操作的代码……
Private Sub mnuFSaveFrom_Click(sender As Object, e As EventArgs) Handles mnuFSaveFrom.Click
My.Settings.m_StartFrom = _myVideo.CurrentPosition
My.Settings.m_StartFromFilename = _strSavedFile
_toBeContinued = True
My.Settings.m_ToBeContinued = _toBeContinued
My.Settings.Save()
End Sub
Private Sub OpenSavedFile(ByVal fName As String)
If fName = "" Or fName = String.Empty Or fName = vbNullString Then Exit Sub
Dim hite As Integer = pnlVideo.Height
Dim wide As Integer = pnlVideo.Width
_myVideo = New Video(fName)
'Set the Owner...
_myVideo.Owner = pnlVideo
'Set the video's size...
pnlVideo.Height = hite
pnlVideo.Width = wide
'Set the starting point of the saved file where you left off.
_myVideo.CurrentPosition = _
_myVideo.SeekCurrentPosition(My.Settings.m_StartFrom, SeekPositionFlags.AbsolutePositioning)
'Set Trackbar values...
gtbPosition.MaxValue = _myVideo.Duration
gtbPosition.Value = _myVideo.CurrentPosition()
'Play it.
_myVideo.Play()
'Show the cursor...
_myVideo.ShowCursor()
'Start the timer...
videoTimer.Enabled = True
'Set the form text
lblFormText.Text = "RSVP 2 - " & fName
'Show the PlayState in a label...
lblStatus.Text = StateFlags.Running.ToString()
'Do some Button and Menu checking
btnPlay.Enabled = False
mnuOPlay.Enabled = False
cmsPlay.Enabled = False
btnPause.Enabled = True
mnuOPause.Enabled = True
cmsPause.Enabled = True
btnStop.Enabled = False
mnuOStop.Enabled = False
cmsStop.Enabled = False
mnuONormalView.Checked = True
mnuOFullScreen.Checked = False
mnuFOpen.Enabled = False
mnuFSaveFrom.Enabled = True
End Sub
Private Sub mnuFOpenSavedFile_Click(sender As Object, e As EventArgs) Handles mnuFOpenSavedFile.Click
If My.Settings.m_ToBeContinued = True Then
OpenSavedFile(My.Settings.m_StartFromFilename)
_toBeContinued = False
My.Settings.m_ToBeContinued = _toBeContinued
My.Settings.m_StartFrom = Nothing
_strSavedFile = ""
My.Settings.m_StartFromFilename = _strSavedFile
My.Settings.Save()
End If
End Sub
障碍
我遇到了一些问题,例如在视频播放时显示光标、将时长格式化为标签、将当前播放时间格式化为标签、在全屏模式下显示上下文菜单以及在面板中播放视频。
让我们开始在面板中播放视频。正如我之前所说,我删除了 Mike 的很多代码,碰巧这也是其中的一部分。所以这是我的版本
Private Sub OpenAviFile()
Dim vTime As String = Nothing
lblTime.Text = "00:00:00"
With ofd
.InitialDirectory = "F:\movz\"
.RestoreDirectory = True
End With
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim height As Integer = pnlVideo.Height
Dim width As Integer = pnlVideo.Width
'Set the video to the selected file.
video = New Video(ofd.FileName)
'Get the file Length in time.
vTime = GetVideoTime(vTime)
'Display the Labels and what is playing.
txtPlaying.Visible = True
txtPlaying.Text = ""
txtPlaying.Text = "<<<--- Now Playing " & ofd.FileName & " --->>>"
lblTime.Text = vTime
fName = ofd.FileName
GetFileLength()
'Set the owner of the video.
video.Owner = pnlVideo
'Set the height and width of the video panel and
'set the progressbar to zero.
pnlVideo.Height = height
pnlVideo.Width = width
videoProgress.Value = 0
'Play the file, enable the timer, set the boolean to true.
video.Play()
lblTimer.Enabled = True
videoPlaying = True
End If
End Sub
接下来,我必须更新 progressbar
和 lblDuration
标签以对应当前播放的视频。
Private Sub lblTimer_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles lblTimer.Tick
'Display the current position on the progressBar.
Dim vPosition As Integer = Convert.ToInt32(video.CurrentPosition * 1000)
Dim vDuration As Integer = Convert.ToInt32(video.Duration * 1000)
If vDuration > 0 Then
videoProgress.Value = Convert.ToInt32((vPosition * 100) / vDuration)
End If
'Display the current time of the video on a label.
lblDuration.Text = Format(Int(video.Duration \ 3600), "00") & ":" & _
Format(Int(video.Duration - video.CurrentPosition) \ 60, "00") & ":" & _
Format(Int((video.Duration - video.CurrentPosition) Mod 60), "00").ToString
End Sub
文件的 MB 大小长度实现起来相当简单。我创建了一个变量 myFile
并将其设置为 FileInfo
。然后我将其初始化为 FileInfo(fName)
,该变量在顶层变量中声明。然后我又创建了一个名为 length
的变量并将其设置为 myFile.Length
。剩下要做的就是将其格式化为 MB。这是代码
Private Sub GetFileLength()
'Gets the length of the selected file.
'This sub is called in OpenAVIFile sub.
Dim myFile As FileInfo
myFile = New FileInfo(fName)
Dim length As Long = myFile.Length()
lblLength.Text = Format(Int(length / 1048000), "000") & " MB"
End Sub
这个程序没有加载很多无用的东西。它只做播放 AVI 文件、显示一些带有数据的标签、调整音量和平衡,或者使用全屏和返回正常视图。就是这样。
屏幕保护程序控制
我添加了 Kurt Shultz 的屏幕保护程序控件,您可以在 CodeProject 上 这里 找到。我改变了窗体的外观以适应我的程序。我还将他的 C# 代码转换成了 VB。这花费了我不少时间。大约 5 个小时。
更新
- 添加了新设计的视频播放器 2012/09/24
- 添加了保存例程,以防您需要匆忙离开。2013/04/06
- 添加了
OpenSaveFile()
子程序,以便在您想恢复播放时使用。2013/04/06
这是我对播放器进行的所有更新列表。添加了菜单检查、快进/快退、Kurts Shultz 的屏幕保护程序控件,该控件负责全屏模式恢复到正常视图。我还添加了一个退出时的输入框,用于检查屏幕保护程序功能是否已重新激活。希望您喜欢这个程序。