vb.net

位置:IT落伍者 >> vb.net >> 浏览文章

用VB.net2008打造你的影音播放器


发布日期:2022年04月04日
 
用VB.net2008打造你的影音播放器

本篇文章的主要开发环境是Visual Studio Visual Studio系列产品一直以来都提供了强大的控件功能然而我们利用这些控件可以编写出功能强大的应用程序本文主要利用微软的最开发工具为大家展示一个应用程序的开发过程让大家对添加/引用控件更加熟悉很适开发工具的初学者具有一定的实用价值

打开 Visual Studio 在文件 (File) 菜单上单击新建项目 (New Project) 在新建项目 (New Project) 对话框的模板 (Templates) 窗格中单击 Windows 应用程序(Windows Application)单击确定 (OK)

由于我们需要以Windows Media Player作为播放控件所以我们需要将Windows Media Player的控件添加到我们的工具箱在此之前请安装最新的Windows Media Player SDK或者Windows Media Player播放器即可一般情况下系统都默认安装了这个播放器如果你确定已经安装了请搜索wmpdll这个文件(一般存在\system\wmpdll)如搜索完成后直接将此控件拖入我们的控件工具箱即可

拖入我们的工具箱

选择此控件拖入我们的Form界面

选择Form窗体在Form窗体中添加如下控件

OpenFileDialog控件Timer控件MenuStripSaveFileDialogFolderBrowserDialog ListBox控件HScrollBar控件

个按钮控件分别为ButtonButtonButton

控件属性设置如下

Button Text: 打开

Button Text: 播放

Button Text: 停止

MenuStrip 添加菜单选项 文件

MenuStrip 菜单选项 打开

MenuStrip 菜单选项 打开目录

MenuStrip 菜单选项 关闭

进入Button_Click事件

Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick

OpenFileDialogInitialDirectory = c:\

OpenFileDialogFilter = mp 文件(*mp)|*mp|CD音频文件(*wav)|*wav| & 视频(*asf)|*asf|所有文件(**)|**

If OpenFileDialogShowDialog = WindowsFormsDialogResultOK Then

AxWindowsMediaPlayerURL = OpenFileDialogFileName

ListBoxItemsAdd(OpenFileDialogFileName)

End If

End Sub

进入Button_Click事件

Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick

If ButtonText = 播放 Then

AxWindowsMediaPlayerCtlcontrolspause()

ButtonText = 暂停

Else

AxWindowsMediaPlayerCtlcontrolsplay()

ButtonText = 播放

End If

End Sub

Private Sub 打开ToolStripMenuItem_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles 打开ToolStripMenuItemClick

Button_Click(sender e)

End Sub

进入Button_Click事件

Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick

AxWindowsMediaPlayerCtlcontrolsstop() 停止

AxWindowsMediaPlayerCtlcontrolscurrentPosition() = 重新开始

AxWindowsMediaPlayerURL =

End Sub

进入 打开ToolStripMenuItem_Click事件

进入打开目录ToolStripMenuItem_Click事件

Private Sub 打开目录ToolStripMenuItem_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles 打开目录ToolStripMenuItemClick

If FolderBrowserDialogShowDialog = WindowsFormsDialogResultOK Then

Dim fi As IOFileInfo

Dim dir As IODirectoryInfo = New IODirectoryInfo(FolderBrowserDialogSelectedPath)

Dim file As String

For Each fi In dirGetFiles(*mp)

file = fiFullName

ListBoxItemsAdd(file)

Next

End If

End Sub

进入关闭ToolStripMenuItem_Click事件

Private Sub 关闭ToolStripMenuItem_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles 关闭ToolStripMenuItemClick

关闭

If MessageBoxShow(请确定你要关闭吗? 关闭 MessageBoxButtonsOKCancel) = WindowsFormsDialogResultOK Then

Close()

Else

Return

End If

End Sub

进入Timer_Tick事件

Private Sub Timer_Tick(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles TimerTick

HScrollBarValue = AxWindowsMediaPlayerCtlcontrolscurrentPosition

End Sub

进入HScrollBar_Scroll事件

Private Sub HScrollBar_Scroll(ByVal sender As SystemObject ByVal e As SystemWindowsFormsScrollEventArgs) Handles HScrollBarScroll

进度条

AxWindowsMediaPlayerCtlcontrolscurrentPosition() = HScrollBarValue

End Sub

进入ListBox_DoubleClick事件

Private Sub ListBox_DoubleClick(ByVal sender As Object ByVal e As SystemEventArgs) Handles ListBoxDoubleClick

AxWindowsMediaPlayerURL = ListBoxSelectedItemToString

End Sub 代码已经输入完毕接下来我们需要运行程序进行测试

好了程序运行成功此播放器已经具备了最基本的功能感兴趣的朋友还可以向程序增加更多的功能

上一篇:VB.NET 2005编写定时关机程序

下一篇:了解VB.NET中的常量与枚举功能