代码贴给你,哈哈,反正是陈年的老东西啦,[
]
''--------------------------------------------------------
''This sample will show how to use the "SaveToFile"
''--------------------------------------------------------
Dim dx As New DirectX7
Dim ds As DirectSound
Dim dsb As DirectSoundBuffer
Dim dsd As DSBUFFERDESC
Dim dsc As DirectSoundCapture
Dim dscb As DirectSoundCaptureBuffer
Dim dscd As DSCBUFFERDESC
Dim CaptureWave As WAVEFORMATEX
Dim capCURS As DSCURSORS
Dim ByteBuffer() As Integer
Dim CNT As Integer
Dim cCaps As DSCCAPS
Dim gfPlay As Boolean
Private Sub cmdPlayRec_Click()
''----------------------------------------
'' convert the data from a capture buffer
'' to a sound buffer
''----------------------------------------
ConvertToSBuffer
'' did the sound buffer get created?
If dsb Is Nothing then
Exit Sub
else
dsb.Play DSBPLAY_DEFAULT
tmrCount.Enabled = True
CNT = 0
lblTIME.Caption = vbNullString
If gfPlay then
cmdStopPlaying.Enabled = True
End If
End Sub
Private Sub cmdSaveToFile_Click()
On Error Resume Next
Dim FileLocal As String
ConvertToSBuffer
If dsb Is Nothing then
Exit Sub
cmdStopPlaying.Enabled = False
tmrCount.Enabled = False
lblTIME.Caption = vbNullString
CNT = 0
If dsb Is Nothing then
MsgBox "Please record a sound first"
Exit Sub
End If
'common dialog control
svFile.Filter = "*.wav"
svFile.DialogTitle = "Save Wave File"
svFile.ShowSave
If Right(svFile.filename, 4) <> ".wav" And svFile.filename <> vbNullString then
FileLocal = svFile.filename
FileLocal = FileLocal &
".wav"
else
FileLocal = svFile.filename
End If
'FileLocal = InputBox("Please enter the location, and file name you want the file saved as.", "SAVE", "c:/windows/temp/test.wav")
If FileLocal = vbNullString then
Exit Sub
If Mid(FileLocal, 2, 1) <> ":" then
Exit Sub
If Right(FileLocal, 3) <> "wav" then
MsgBox "Please enter a correct name ie something.wav", vbApplicationModal
Exit Sub
End If
dsb.SaveToFile FileLocal
End Sub
Private Sub cmdStartRec_Click()
Set dscb = Nothing
Call InitCapture
dscb.start DSCBSTART_DEFAULT
tmrCount.Interval = 1000
tmrCount.Enabled = True
cmdStopRec.Enabled = True
cmdStartRec.Enabled = False
End Sub
Private Sub cmdStopPlaying_Click()
If dsb Is Nothing then
Exit Sub
Dim l_st As Long
Dim l_soundStatus As Long
''--- see if the capture buffer is running
l_st = dscb.GetStatus()
If (l_st And DSCBSTATUS_CAPTURING) then
dscb.Stop
End If
''-- see if the sound buffer is playing
l_soundStatus = dsb.GetStatus()
If (l_soundStatus And DSBSTATUS_PLAYING) then
dsb.Stop
dsb.SetCurrentPosition 0
End If
tmrCount.Enabled = False
CNT = 0
lblTIME.Caption = vbNullString
cmdStopPlaying.Enabled = False
End Sub
Private Sub cmdStopRec_Click()
Dim l_bufferS As Long
If dscb Is Nothing then
Exit Sub
cmdSaveToFile.Enabled = True
If gfPlay then
cmdPlayRec.Enabled = True
''cmdStopPlaying.Enabled = True
'' is the buffer going?
l_bufferS = dscb.GetStatus()
If (l_bufferS And DSCBSTATUS_CAPTURING) then
dscb.Stop
End If
tmrCount.Enabled = False
CNT = 0
lblTIME.Caption = vbNullString
cmdStartRec.Enabled = True
cmdStopRec.Enabled = False
End Sub
Private Sub Form_Load()
On Local Error GoTo errOut
Set dsc = dx.DirectSoundCaptureCreate(vbNullString)
On Error Resume Next
Set ds = dx.DirectSoundCreate(vbNullString)
If Err.Number = DSERR_ALLOCATED then
'The card isn't supporting full duplex
gfPlay = False
MsgBox "This carddo
es not support full duplex. You may still record sound.", vbOKOnly Or vbInformation, "No full duplex"
else
gfPlay = True
ds.SetCooperativeLevel Me.hWnd, DSSCL_NORMAL
End If
On Local Error GoTo errOut
InitCapture
cmdSaveToFile.Enabled = False
cmdPlayRec.Enabled = False
cmdStopPlaying.Enabled = False
cmdStopRec.Enabled = False
lblTIME.Caption = vbNullString
Exit Sub
errOut:
MsgBox "Unable to initialize sound card for capture. Exiting this application", vbOKOnly Or vbCritical
End
End Sub
Private Sub ConvertToSBuffer()
Dim l_captureS As Long
'' are we running?
l_captureS = dscb.GetStatus()
If (l_captureS And DSCBSTATUS_CAPTURING) then
dscb.Stop
End If
'' get the capture info
dscb.GetCurrentPosition capCURS
dsd.lBufferBytes = capCURS.lWrite * dscd.fxFormat.nBlockAlign
dsd.lFlags = DSBCAPS_CTRLVOLUME Or DSBCAPS_STATIC
If capCURS.lWrite = 0 then
Exit Sub
End If
Set dsb = ds.CreateSoundBuffer(dsd, dscd.fxFormat)
ReDim ByteBuffer(capCURS.lWrite * dscd.fxFormat.nBlockAlign + 1)
dscb.ReadBuffer 0, capCURS.lWrite * dscd.fxFormat.nBlockAlign, ByteBuffer(0), DSCBLOCK_DEFAULT
dsb.WriteBuffer 0, capCURS.lWrite * dscd.fxFormat.nBlockAlign, ByteBuffer(0), DSBLOCK_DEFAULT
''Set dscb = Nothing
End Sub
Private Function WaveEx(Hz As Long, Channels As Integer, BITS As Integer) As WAVEFORMATEX
WaveEx.nFormatTag = WAVE_FORMAT_PCM
WaveEx.nChannels = Channels
WaveEx.lSamplesPerSec = Hz
WaveEx.nBitsPerSample = BITS
WaveEx.nBlockAlign = Channels * BITS / 8
WaveEx.lAvgBytesPerSec = WaveEx.lSamplesPerSec * WaveEx.nBlockAlign
WaveEx.nSize = 0
End Function
Private Sub InitCapture()
'set the capture buffer
dsc.GetCaps cCaps
If cCaps.lFormats And WAVE_FORMAT_2M08 then
CaptureWave = WaveEx(22050, 1, 8)
else
If cCaps.lFormats And WAVE_FORMAT_1M08 then
CaptureWave = WaveEx(11025, 1, 8)
else
MsgBox "Capture is not supported with your sound card!", vbApplicationModal
End
End If
dscd.fxFormat = CaptureWave
dscd.lBufferBytes = CaptureWave.lAvgBytesPerSec * 20
dscd.lFlags = DSCBCAPS_WAVEMAPPED
Set dscb = dsc.CreateCaptureBuffer(dscd)
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Call CleanUp
End
End Sub
Private Sub CleanUp()
''Clean up all the stuff
Set dx = Nothing
Set ds = Nothing
Set dsb = Nothing
Set dsc = Nothing
Set dscb = Nothing
Erase ByteBuffer
End Sub
Private Sub mnuExit_Click()
Unload Me
End Sub
Private Sub tmrCount_Timer()
On Error Resume Next
CNT = CNT + 1
If CNT = 19 then
dscb.Stop
lblTIME.Caption = "Full"
frmMain.Refresh
tmrCount.Enabled = False
cmdSaveToFile.Enabled = True
If gfPlay then
cmdPlayRec.Enabled = True
If gfPlay then
cmdStopPlaying.Enabled = True
Exit Sub
End If
lblTIME.Caption = CNT
''check the status of the sound buffer
Dim l_sBs As Long
If Not (dsb Is Nothing) then
l_sBs = dsb.GetStatus()
If (l_sBs And DSBSTATUS_PLAYING) then
else
If cmdStartRec.Enabled = True then
tmrCount.Enabled = False
CNT = 1
lblTIME.Caption = vbNullString
cmdStopPlaying.Enabled = False
End If
End If
End If
End Sub