十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
工具栏里选择"添加,删除组件"
创新互联公司专注于福鼎网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供福鼎营销型网站建设,福鼎网站制作、福鼎网页设计、福鼎网站官网定制、微信小程序开发服务,打造福鼎网络公司原创品牌,更为您提供福鼎网站排名全网营销落地服务。
选择com组件
选择"Windows Media Player "
然后在界面上加入这个组件
代码
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Const DATA_FILE_EXTENSION As String = ".mp3"
Dim dlgFileDialog As New OpenFileDialog
With dlgFileDialog
.Filter = DATA_FILE_EXTENSION _
" files (*" DATA_FILE_EXTENSION "|*" DATA_FILE_EXTENSION
.FilterIndex = 1
.RestoreDirectory = True
If .ShowDialog() = DialogResult.OK Then
'Play the sound file
Me.AxWindowsMediaPlayer1.URL = dlgFileDialog.FileName
End If
End With
End Sub
参考资料中可以看到很详细的步骤
“工具箱”中单击右键,选择“选择项”菜单,打开“选择工具箱项”窗口,选择“COM组件”标签,在列表中找到并勾选“Windows Media Player”组件,单击“确定”按钮。将该组件添加到指定的工具箱选项卡中,然后在工具箱里面找 Windows Media Player 控件,拉到form里面,拉出来的控件就是AxWindowsMediaPlayer
我这里有一段前段时间写的测试代码,使用WMPLib类,中间有你需要的功能,你可以参考下:
使用wmp.currentMedia.duration和wmp.currentMedia.durationString分别以double和string获得当前媒体的播放时间,使用wmp.settings.volume设置音量大小,使用wmp.controls.currentPosition设置当前播放时间点
Imports WMPLib
Public Class FrmMain
Dim WithEvents wmp As WMPLib.WindowsMediaPlayer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
wmp = New WMPLib.WindowsMediaPlayer()
TextBox1.Text = TextBox1.Text vbCrLf "当前播放状态:" wmp.status
wmp.uiMode = "Mini"
wmp.settings.balance = 1
wmp.enableContextMenu = True
wmp.fullScreen = False
wmp.windowlessVideo = True
wmp.enabled = True
Dim wmpMediaList As IWMPPlaylist = wmp.newPlaylist("默认播放列表", "")
Dim wmpMedia As IWMPMedia = wmp.newMedia(My.Computer.FileSystem.CurrentDirectory "\Human.mp3")
With wmpMediaList
.appendItem(wmpMedia)
End With
'wmp.URL = "\\192.168.1.247\sharoncn\music\Human.mp3"
wmp.currentMedia = wmpMedia
wmp.settings.autoStart = True
tBarPlay.Value = 0
ListBox1.Items.Add(wmpMediaList.name)
ListBox1.SelectedIndex = 0
ListBox2.Items.Add(wmpMediaList.Item(0).name)
ListBox2.SelectedIndex = 0
tbar.Maximum = 100
tbar.Value = 50
Timer1.Enabled = True
Timer1.Interval = 100
End Sub
Private Sub wmp_PlayStateChange(ByVal NewState As Integer) Handles wmp.PlayStateChange
tBarPlay.Maximum = wmp.currentMedia.duration * 10
Label1.Text = "总时间:" wmp.currentMedia.durationString
TextBox1.Text = TextBox1.Text vbCrLf "当前播放状态:" NewState
If NewState = 1 Then
wmp.controls.play()
End If
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
TextBox1.SelectionStart = Len(TextBox1.Text)
TextBox1.ScrollToCaret()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label2.Text = "当前播放进度:" wmp.controls.currentPositionString
tBarPlay.Value = CInt(wmp.controls.currentPosition * 10)
End Sub
Private Sub tbar_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbar.Scroll
wmp.settings.volume = tbar.Value
End Sub
Private Sub tBarPlay_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tBarPlay.Scroll
wmp.controls.currentPosition = tBarPlay.Value / 10
End Sub
End Class
'在窗体上添加一个按钮测试
Private
m_PlayFlag
As
Boolean
=
False
'是否正在播放
Private
Sub
Button1_Click(ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
Button1.Click
If
m_PlayFlag
=
False
Then
Button1.Image
=
Image.FromFile("d:\play.jpg")
'更换为播放图标
m_PlayFlag
=
True
'改变标志
Else
Button1.Image
=
Image.FromFile("d:\pause.jpg")
'更换为暂停图标
m_PlayFlag
=
False
'改变标志
End
If
End
Sub
Private
Sub
Form1_Load(ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
MyBase.Load
'窗体加载时使用暂停图标
Button1.Image
=
Image.FromFile("d:\pause.jpg")
End
Sub
My.Computer.Audio.Play("SoundFile.wav")
SoundFile.wav是你要播放的声音文件的路径