My.Computer.Audio.Play("SoundFile.wav")
成都創(chuàng)新互聯(lián)主要從事網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)奉賢,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220
SoundFile.wav是你要播放的聲音文件的路徑
準(zhǔn)備采集 哪方面的內(nèi)容呢?
我使用 VB 編寫了 搜狐論壇的圖片采集,搜狐博客的文檔下載,百度和谷歌的圖片搜索采集,和訊博客的自動(dòng)訪問,某一投票的自動(dòng)投票成尋。
你采集哪方面的數(shù)據(jù)呢?可以交流一下。
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
具體還有很多常量,不一一列舉,下面是播放一個(gè)wav文件的例子
Dim mstrfileName As String = "c:\eagle2.wav"
PlaySound(mstrfileName, 0, SND_FILENAME)
注意上面的播放是非同步的,就是說它并不會(huì)播放完畢才結(jié)束。如果把上面的代碼寫在一個(gè)Button.Click的事件處理程序中,第二次點(diǎn)擊的時(shí)候它會(huì)打斷第一次的播放,重新開始。
當(dāng)然我們也可以調(diào)用系統(tǒng)的聲音:
PlaySound("SystemStart", 0, SND_ALIAS Or SND_SYNC)
' 以同步的方式調(diào)用系統(tǒng)啟動(dòng)時(shí)候的聲音。
2. 調(diào)用媒體播放器控件
VB.NET中并沒有提供媒體播放器的.NET組件,沒辦法我們還是調(diào)用以前Com組件Windows Media Player。當(dāng)然除了聲音外,還可以播放視頻文件。
首先把媒體播放器控件加到工具欄中,調(diào)用還是簡單的寫一下吧:)
Private Sub playMediaFile(ByVal mediaFileName As String)
With MediaPlayer1
.Stop()
.FileName = "c:\mp3\愛不愛我.mp3"
.Play()
End With
End Sub
方法一:調(diào)用系統(tǒng)DLL(這個(gè)有點(diǎn)不完善,也就是播放聲音時(shí)窗體無法控制,效果相當(dāng)于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)
先添加一個(gè)windowsmediaplayer控件(工具箱里沒有,需要手動(dòng)添加)
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
上面的都是點(diǎn)擊button1(按鈕1)后播放聲音,若是鼠標(biāo)移動(dòng)到某控件上發(fā)出聲音的話改一下觸發(fā)事件就OK了。
不明白你為什么要二進(jìn)制讀取聲音文件,VB可以直接調(diào)用API播放聲音文件,不需要你二進(jìn)制來讀取聲音文件的。
Private
Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA"
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub PlayWavFile(strFileName As String, PlayCount As Long, JianGe As Long)
'strFileName 要播放的文件名(帶路徑)
'playCount 播放的次數(shù)
'JianGe 多次播放時(shí),每次的時(shí)間間隔
If Len(Dir(strFileName)) = 0 Then Exit Sub
If PlayCount = 0 Then Exit Sub
If JianGe 1000 Then JianGe = 1000
DoEvents
sndPlaySound strFileName, 16 + 1
Sleep JianGe
Call PlayWavFile(strFileName, PlayCount - 1, JianGe)
End Sub
Private Sub Form_Click()
PlayWavFile "C:\aaa.wav", 1, 1000 '參數(shù):播放文件,播放次數(shù),播放間隔
End Sub
A simple Sound Recorder~
1. Add Three buttons (button1, button2, button3) and a label (label1) to the form.
2. Set the Text of Button1 to Start, Button2 to Stop, and Button3 to Play.
3. Add this code:
Public?Class?Form1
Private?Declare?Function?mciSendString?Lib?"winmm.dll"?Alias?"mciSendStringA"?(ByVal?lpstrCommand?As?String,?ByVal?lpstrReturnString?As?String,?ByVal?uReturnLength?As?Integer,?ByVal?hwndCallback?As?Integer)?As?Integer
Private?Sub?Button1_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button1.Click
Button1.Enabled?=?False
Button2.Enabled?=?True
mciSendString("open?new?Type?waveaudio?Alias?recsound",?"",?0,?0)
mciSendString("record?recsound",?"",?0,?0)
Label1.Text?=?"Recording..."
Label1.Visible?=?True
End?Sub
Private?Sub?Button2_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button2.Click
Button1.Enabled?=?True
Button2.Enabled?=?False
Button3.Enabled?=?True
mciSendString("save?recsound?c:\recsound.wav",?"",?0,?0)
mciSendString("close?recsound",?"",?0,?0)
MsgBox("File?Created:?C:\recsound.wav")
Label1.Text?=?"Stopped..."
Label1.Visible?=?False
My.Computer.Audio.Stop()
End?Sub
Private?Sub?Button3_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button3.Click
Label1.Text?=?"Playing..."
Label1.Visible?=?True
My.Computer.Audio.Play("c:\recsound.wav",?AudioPlayMode.Background)
End?Sub
End?Class