用VB.net2008打造你的影音播放器
【打印文章】
本篇文章的主要开发环境是Visual Studio 2008,Visual Studio系列产品一直以来都提供了强大的控件功能,然而我们利用这些控件可以编写出功能强大的应用程序。本文主要利用微软的最新.net开发工具为大家展示一个应用程序的开发过程,让大家对添加/引用控件更加熟悉,很适合.net开发工具的初学者,具有一定的实用价值。
打开 Visual Studio 2008在文件 (File) 菜单上,单击新建项目 (New Project)。 在新建项目 (New Project) 对话框的模板 (Templates) 窗格中,单击 Windows 应用程序(Windows Application)。单击确定 (OK)
由于我们需要以Windows Media Player作为播放控件,所以我们需要将Windows Media Player的控件添加到我们的工具箱,在此之前请安装最新的Windows Media Player SDK或者Windows Media Player播放器即可,一般情况下系统都默认安装了这个播放器。如果你确定已经安装了请搜索wmp.dll这个文件(一般存在\system32 \wmp.dll),如搜索完成后直接将此控件拖入我们的控件工具箱即可 。
拖入我们的工具箱
选择此控件拖入我们的Form1界面
选择Form1窗体,在Form1窗体中添加如下控件:
OpenFileDialog1控件、Timer1控件、MenuStrip1、SaveFileDialog1、FolderBrowserDialog1 ListBox1控件、HScrollBar1控件
3个按钮控件分别为:Button1、Button2、Button3
控件属性设置如下:
Button1 Text: 打开
Button2 Text: 播放
Button3 Text: 停止
MenuStrip1 添加菜单选项 文件
MenuStrip1 菜单选项 打开
MenuStrip1 菜单选项 打开目录
MenuStrip1 菜单选项 关闭
进入Button1_Click事件
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.InitialDirectory = "c:\"
OpenFileDialog1.Filter = "mp3 文件(*.mp3)|*.mp3|CD音频文件(*.wav)|*.wav|" & "视频(*.asf)|*.asf|所有文件(*.*)|*.*"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
AxWindowsMediaPlayer1.URL = OpenFileDialog1.FileName
ListBox1.Items.Add(OpenFileDialog1.FileName)
End If
End Sub
进入Button2_Click事件
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Button2.Text = "播放" Then
AxWindowsMediaPlayer1.Ctlcontrols.pause()
Button2.Text = "暂停"
Else
AxWindowsMediaPlayer1.Ctlcontrols.play()
Button2.Text = "播放"
End If
End Sub
Private Sub 打开ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 打开ToolStripMenuItem.Click
Button1_Click(sender, e)
End Sub
进入Button3_Click事件
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
AxWindowsMediaPlayer1.Ctlcontrols.stop() '停止
AxWindowsMediaPlayer1.Ctlcontrols.currentPosition() = 0 '重新开始
AxWindowsMediaPlayer1.URL = ""
End Sub
进入 打开ToolStripMenuItem_Click事件
进入打开目录ToolStripMenuItem_Click事件
Private Sub 打开目录ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 打开目录ToolStripMenuItem.Click
If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim fi As IO.FileInfo
Dim dir As IO.DirectoryInfo = New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
Dim file As String
For Each fi In dir.GetFiles("*.mp3")
file = fi.FullName
ListBox1.Items.Add(file)
Next
End If
End Sub
进入关闭ToolStripMenuItem_Click事件
Private Sub 关闭ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 关闭ToolStripMenuItem.Click
''关闭
If MessageBox.Show("请确定你要关闭吗?", "关闭", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK Then
Close()
Else
Return
End If
End Sub
进入Timer1_Tick事件
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
HScrollBar1.Value = AxWindowsMediaPlayer1.Ctlcontrols.currentPosition
End Sub
进入HScrollBar1_Scroll事件
Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
''进度条
AxWindowsMediaPlayer1.Ctlcontrols.currentPosition() = HScrollBar1.Value
End Sub
进入ListBox1_DoubleClick事件
Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem.ToString
End Sub
代码已经输入完毕,接下来我们需要运行程序进行测试。
好了程序运行成功,此播放器已经具备了最基本的功能。感兴趣的朋友还可以向程序增加更多的功能。
打开 Visual Studio 2008在文件 (File) 菜单上,单击新建项目 (New Project)。 在新建项目 (New Project) 对话框的模板 (Templates) 窗格中,单击 Windows 应用程序(Windows Application)。单击确定 (OK)
由于我们需要以Windows Media Player作为播放控件,所以我们需要将Windows Media Player的控件添加到我们的工具箱,在此之前请安装最新的Windows Media Player SDK或者Windows Media Player播放器即可,一般情况下系统都默认安装了这个播放器。如果你确定已经安装了请搜索wmp.dll这个文件(一般存在\system32 \wmp.dll),如搜索完成后直接将此控件拖入我们的控件工具箱即可 。
拖入我们的工具箱
选择此控件拖入我们的Form1界面
选择Form1窗体,在Form1窗体中添加如下控件:
OpenFileDialog1控件、Timer1控件、MenuStrip1、SaveFileDialog1、FolderBrowserDialog1 ListBox1控件、HScrollBar1控件
3个按钮控件分别为:Button1、Button2、Button3
控件属性设置如下:
Button1 Text: 打开
Button2 Text: 播放
Button3 Text: 停止
MenuStrip1 添加菜单选项 文件
MenuStrip1 菜单选项 打开
MenuStrip1 菜单选项 打开目录
MenuStrip1 菜单选项 关闭
进入Button1_Click事件
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.InitialDirectory = "c:\"
OpenFileDialog1.Filter = "mp3 文件(*.mp3)|*.mp3|CD音频文件(*.wav)|*.wav|" & "视频(*.asf)|*.asf|所有文件(*.*)|*.*"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
AxWindowsMediaPlayer1.URL = OpenFileDialog1.FileName
ListBox1.Items.Add(OpenFileDialog1.FileName)
End If
End Sub
进入Button2_Click事件
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Button2.Text = "播放" Then
AxWindowsMediaPlayer1.Ctlcontrols.pause()
Button2.Text = "暂停"
Else
AxWindowsMediaPlayer1.Ctlcontrols.play()
Button2.Text = "播放"
End If
End Sub
Private Sub 打开ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 打开ToolStripMenuItem.Click
Button1_Click(sender, e)
End Sub
进入Button3_Click事件
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
AxWindowsMediaPlayer1.Ctlcontrols.stop() '停止
AxWindowsMediaPlayer1.Ctlcontrols.currentPosition() = 0 '重新开始
AxWindowsMediaPlayer1.URL = ""
End Sub
进入 打开ToolStripMenuItem_Click事件
进入打开目录ToolStripMenuItem_Click事件
Private Sub 打开目录ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 打开目录ToolStripMenuItem.Click
If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim fi As IO.FileInfo
Dim dir As IO.DirectoryInfo = New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
Dim file As String
For Each fi In dir.GetFiles("*.mp3")
file = fi.FullName
ListBox1.Items.Add(file)
Next
End If
End Sub
进入关闭ToolStripMenuItem_Click事件
Private Sub 关闭ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 关闭ToolStripMenuItem.Click
''关闭
If MessageBox.Show("请确定你要关闭吗?", "关闭", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK Then
Close()
Else
Return
End If
End Sub
进入Timer1_Tick事件
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
HScrollBar1.Value = AxWindowsMediaPlayer1.Ctlcontrols.currentPosition
End Sub
进入HScrollBar1_Scroll事件
Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
''进度条
AxWindowsMediaPlayer1.Ctlcontrols.currentPosition() = HScrollBar1.Value
End Sub
进入ListBox1_DoubleClick事件
Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem.ToString
End Sub
代码已经输入完毕,接下来我们需要运行程序进行测试。
好了程序运行成功,此播放器已经具备了最基本的功能。感兴趣的朋友还可以向程序增加更多的功能。
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )
【编程爱好者论坛】