Thứ Tư, 26 tháng 8, 2015

Lưu âm thanh từ micro, cd-line (VB)

Câu hỏi :
Lưu âm thanh từ micro, cd-line (VB)

Trả lời :
Hôm nay chúng tôi xin giới thiệu với các bạn cách để lưu âm thanh từ micro, cd-line.. vào tập tin. Đoạn code rất hữu dụng khi bạn viết chương trình media.


Lưu file dưới dạng wav

' -=-=-=- METHODS -=-=-=-=-
' PauseRecord Toggle để pause the record (Nếu bạn đang thu)
' StartRecord Start Recording
' StopRecord Stop recording
'-------------------------------------------------------------

Private FName As String
Private Declare Function mciSendString Lib ''winmm.dll'' Alias ''mciSendStringA'' (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Public Enum MyState
    Idle
    Recording
    Paused
End Enum

Private xState As MyState

Public Property Get FileName() As String
    FileName = FName
End Property

Public Property Let FileName(ByVal sFileName As String)
    FName = sFileName
End Property

Public Function StartRecord() As Boolean
On Error GoTo ER:
If FName = '''' Then GoTo ER:
    Dim RS As String, cb As Long, I As Long
    RS = Space$(128)
    I = mciSendString(''open new type waveaudio alias capture'', RS, 128, cb)
    I = mciSendString(''record capture'', RS, 128, cb)
    xState = Recording
    StartRecord = True
    Exit Function
ER:
StartRecord = False
End Function

Public Function StopRecord() As Boolean
    On Error GoTo ER:
    If FName = '''' Then GoTo ER:
        Dim RS As String, cb As Long, I As Long
        RS = Space$(128)
        I = mciSendString(''save capture '' & FName, RS, 128, cb)
        I = mciSendString(''close capture'', RS, 128, cb)
        xState = Idle
        StopRecord = True
        Exit Function
ER:
I = mciSendString(''close capture'', RS, 128, cb)
StopRecord = False
End Function

Private Sub Class_Initialize()
    xState = Idle
End Sub

Private Sub Class_Terminate()
    StopRecord
End Sub



Public Function PauseRecord() As Boolean
On Error GoTo ER:
    If FName = '''' Then GoTo ER:
        Dim RS As String, cb As Long, I As Long
        RS = Space$(128)
        If xState = Paused Then
        I = mciSendString(''record capture'', RS, 128, cb)
        xState = Recording
    ElseIf xState = Recording Then
        I = mciSendString(''pause capture'', RS, 128, cb)
        xState = Paused
End If
        PauseRecord = True
        Exit Function
ER:
PauseRecord = False
End Function


Public Property Get State() As MyState
    State = xState
End Property

Theo pcworld.com.vn

Không có nhận xét nào:

Đăng nhận xét