Results 1 to 9 of 9

Thread: Recording and playing sound

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2000
    Posts
    71

    Question

    Is there a way to record .wav file & at the same time can detect if playing back of the wav file has finished ? The detection of the playing back can be coded in a true or false satement.

  2. #2
    Megatron
    Guest
    Here's how you can record a WAV file.
    Code:
    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As Any, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
                                           
    Private Sub cmdRecord_Click()
        mciSendString "open new type waveaudio Alias record", 0&, 0, 0
        mciSendString "record record", 0&, 0, 0
    End Sub
    
    Private Sub cmdSave_Click()
        mciSendString "stop record", 0&, 0, 0
        mciSendString "save record C:\Windows\Desktop\MyWav.wav", 0&, 0, 0
        mciSendString "close record", 0&, 0, 0                             
    End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2000
    Posts
    71
    But how to detect if the .wav has finished playing back ?

  4. #4
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    Code:
    'To play a music file
    'Assuming that you have executed Megatron's code
    
    Private Sub cmdPlay_Click()
        mciSendString "open C:\Windows\Desktop\MyWav.wav type waveaudio Alias record", 0&, 0, 0
        mciSendString "Play Record Notify", 0&,0, cmdPlay.hwnd
    End Sub
    
    Private Sub Form_Load()
    'Set a hook for intercepting messages to cmdPlay
        OldProc = SetWindowLong(cmdPlay.hwnd, GWL_WNDPROC, AddressOf WindowProc)
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        SetWindowLong cmdPlay.hwnd, GWL_WNDPROC, OldProc
    End Sub
    
    'in a standard module
    Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    Public Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
    Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Public Const GWL_WNDPROC = (-4)
    Public Const MM_MCINOTIFY = &H3B9
    Global Oldproc As Long
    
    Public Function WindowProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As LongEnd Sub
        If uMsg = MM_MCINOTIFY Then
            'do whatever
            Debug.Print "Song Playback Completed"
        Else
            CallWindowProc OldProc, cmdPlay.hwnd, uMsg, wParam, lParam
        End If
    End Function
    This code may have errors as I have not tested it. I am just wriring what I reember on top of my mind.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2000
    Posts
    71

    Question

    Thanks for replying i alter some of your code. But i noticed that this mcisendstring command do not return until it has finished playing the wav. Is there a way i can call the mcisendstring to play the wav & also allow the exectuion of the remaining code to continue. I mean i was hoping for this mcisendstring to be asynchronous.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2000
    Posts
    71

    Question

    f = mciSendString("open " & sWAV & " type waveaudio alias voice1", 0&, 0, 0)
    f = mciSendString("play voice1", 0&, 0, 0)

    I dont know why the VB complier says that the line' f = mciSendString("play voice1", 0&, 0, 0)' is out of context. What is meant by this & can it be solved?

  7. #7
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    Try using the notify keyword along with the code that I have given. It should work asynchronously. Does the first line execute perfectly or does it reurn any error?

  8. #8
    Megatron
    Guest
    mciSendString("play voice1", 0&, 0, 0)

    Shouldn't you change the last argument to the handle of the Form you want to notify?

  9. #9
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    Yes, definitely you need to set the last argument as a handle to the window that will recieve the notification . I heve used the command button for this purpose and set a hook to trap the MM_MCINOTIFY message. Thats what I have done in the my code posted above.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width