方法一:調(diào)用系統(tǒng)DLL(這個有點不完善,也就是播放聲音時窗體無法控制,效果相當于Enabled=true,但音頻播放完畢后就沒事了)
創(chuàng)新互聯(lián)建站憑借專業(yè)的設(shè)計團隊扎實的技術(shù)支持、優(yōu)質(zhì)高效的服務(wù)意識和豐厚的資源優(yōu)勢,提供專業(yè)的網(wǎng)站策劃、成都做網(wǎng)站、成都網(wǎng)站設(shè)計、網(wǎng)站優(yōu)化、軟件開發(fā)、網(wǎng)站改版等服務(wù),在成都十載的網(wǎng)站建設(shè)設(shè)計經(jīng)驗,為成都上千多家中小型企業(yè)策劃設(shè)計了網(wǎng)站。
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)后播放聲音,若是鼠標移動到某控件上發(fā)出聲音的話改一下觸發(fā)事件就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給予更多的控制,如結(jié)束進程的方法Kill
1.用Win32 API PlaySound
PlaySound位于"winmm.dll"中,可以根據(jù)輸入?yún)?shù)的不同,播放WAV類型的音樂。在VB.NET中調(diào)用跟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)
注意上面的播放是非同步的,就是說它并不會播放完畢才結(jié)束。如果把上面的代碼寫在一個Button.Click的事件處理程序中,第二次點擊的時候它會打斷第一次的播放,重新開始。
當然我們也可以調(diào)用系統(tǒng)的聲音:
PlaySound("SystemStart", 0, SND_ALIAS Or SND_SYNC)
' 以同步的方式調(diào)用系統(tǒng)啟動時候的聲音。
2. 調(diào)用媒體播放器控件
VB.NET中并沒有提供媒體播放器的.NET組件,沒辦法我們還是調(diào)用以前Com組件Windows Media Player。當然除了聲音外,還可以播放視頻文件。
首先把媒體播放器控件加到工具欄中,調(diào)用還是簡單的寫一下吧:)
Private Sub playMediaFile(ByVal mediaFileName As String)
With MediaPlayer1
.Stop()
.FileName = "c:\mp3\愛不愛我.mp3"
.Play()
End With
End Sub