扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
方法一:调用系统DLL(这个有点不完善,也就是播放声音时窗体无法控制,效果相当于Enabled=true,但音频播放完毕后就没事了)
创新互联建站凭借专业的设计团队扎实的技术支持、优质高效的服务意识和丰厚的资源优势,提供专业的网站策划、成都做网站、成都网站设计、网站优化、软件开发、网站改版等服务,在成都十载的网站建设设计经验,为成都上千多家中小型企业策划设计了网站。
Private Declare Function sndPlaySound Lib "Winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SoundFile As String, Result As Long
SoundFile = "D:\KuGou\Pianoboy - 第105天.wav" '此处为路径
Result = sndPlaySound(SoundFile, 0)
End Sub
方法二:使用WindowsMediaPlayer(WindowsMediaPlayer1的可见为False)
先添加一个windowsmediaplayer控件(工具箱里没有,需要手动添加)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WindowsMediaPlayer1.visible=False
Me.WindowsMediaPlayer1.URL = "D:\KuGou\Pianoboy - 第105天.mp3"
End Sub
上面的都是点击button1(按钮1)后播放声音,若是鼠标移动到某控件上发出声音的话改一下触发事件就OK了。
用Console.Beep(int,int)这个方法就可以了。
另外,建议把提示出错的代码直接放入Catch中,提示鸣放的代码放入Try中。
给你我的程序,自己研究一下吧,我也不是很明白。
Public Class Form1
Private Declare Auto Function PlaySound Lib "winmm.dll" (ByVal lpszSoundName As String, ByVal hModule As Integer, ByVal dwFlags As Integer) As Integer
Const SND_filename As Integer = H20000
Const SND_alias As Integer = H10000
Const SND_sync As Integer = H0
Declare Auto Function waveOutGetNumDevs Lib "winmm.dll" Alias "waveOutGetNumDevs" () As Integer
Declare Auto Function sndPlaySound Lib "winmm.dll" (ByVal filename As String, ByVal options As Integer) As Integer
Private Const SND_memory As Integer = H4
Private Const SND_async As Integer = H1
Private Const proess_domain As String = ""
Const SND_sync1 = H0
Const SND_async1 = H1
Const SND_nodefault = H2
Const SND_loop = H8
Const SND_nostop = H10
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Beep()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim strFileName As String = "start.wav"
PlaySound(strFileName, 0, SND_filename)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
PlaySound("systemstart", 0, SND_alias Or SND_sync)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim ret As Long
Dim strfilename As String
strfilename = "start.wav"
ret = sndPlaySound(strfilename.ToCharArray, 0)
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim i As Integer
i = waveOutGetNumDevs()
If i 0 Then
MsgBox("可以播放声音", 64, "检测")
Else
MsgBox("不能播放声音", 64, "检测")
End If
End Sub
End Class
两种方法
1.
Shell(""Explorer.exe", AppWinStyle.NormalFocus")
2.
Dim proc As New Process
proc.StartInfo.FileName="Explorer.exe"
proc.Start
方法2给予更多的控制,如结束进程的方法Kill
1.用Win32 API PlaySound
PlaySound位于"winmm.dll"中,可以根据输入参数的不同,播放WAV类型的音乐。在VB.NET中调用跟VB6中差不多:
先声明:
Private Declare Auto Function PlaySound Lib "winmm.dll" (ByVal lpszSoundName As String, ByVal hModule As Integer, ByVal dwFlags As Integer) As Integer
Const SND_FILENAME As Integer = H20000
Const SND_ALIAS As Integer = H10000
Const SND_SYNC As Integer = H0
具体还有很多常量,不一一列举,下面是播放一个wav文件的例子
Dim mstrfileName As String = "c:\eagle2.wav"
PlaySound(mstrfileName, 0, SND_FILENAME)
注意上面的播放是非同步的,就是说它并不会播放完毕才结束。如果把上面的代码写在一个Button.Click的事件处理程序中,第二次点击的时候它会打断第一次的播放,重新开始。
当然我们也可以调用系统的声音:
PlaySound("SystemStart", 0, SND_ALIAS Or SND_SYNC)
' 以同步的方式调用系统启动时候的声音。
2. 调用媒体播放器控件
VB.NET中并没有提供媒体播放器的.NET组件,没办法我们还是调用以前Com组件Windows Media Player。当然除了声音外,还可以播放视频文件。
首先把媒体播放器控件加到工具栏中,调用还是简单的写一下吧:)
Private Sub playMediaFile(ByVal mediaFileName As String)
With MediaPlayer1
.Stop()
.FileName = "c:\mp3\爱不爱我.mp3"
.Play()
End With
End Sub
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流