none
Record audio file from a command button on a form RRS feed

  • Question

  • Is there a way to record an audio file when a command button is clicked on a form? And then once the audio file has been recorded, make it a part of the form's record? Maybe as an attachment of some type?

    In other words, the user clicks a command button, an audio recorder opens, the user records the audio file and then when done, that audio file is stored to a directory on the computer. Then the audio file path is stored in a text box on the form. The user then could double click the text box to play back the audio file at any time.

    Anyone?

    --Steve

    Steve C. Leigh

    Tuesday, August 25, 2015 10:42 PM

All replies

  • >>>Is there a way to record an audio file when a command button is clicked on a form? And then once the audio file has been recorded, make it a part of the form's record? Maybe as an attachment of some type?

    This forum is for discussing about Excel for Developers, because your issue is about VBA that is outside the scope of this forum.
    So I will suggest you confirming this issue on the VBA forum.

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=isvvba

    But you could add this functionality is through the MCI commands in the Windows API as defined in the winmm.dll library. The single function mciSendString can execute all of the necessary commands to record and play sound files
    You could put these codes in a module to record an audio file, then change module's name to sndmodule:

    Option Compare Database
    Option Explicit
    
    Public Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
        (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
    
    Const SND_SYNC = &H0
    Const SND_ASYNC = &H1
    Const SND_FILENAME = &H20000
    
    Public 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 BitsPerSec
      Bits16 = 16
      Bits8 = 8
    End Enum
    
    Public Enum SampelsPerSec
      Sampels8000 = 8000
      Sampels11025 = 11025
      Sampels12000 = 12000
      Sampels16000 = 16000
      Sampels22050 = 22050
      Sampels24000 = 24000
      Sampels32000 = 32000
      Sampels44100 = 44100
      Sampels48000 = 48000
    End Enum
    
    Public Enum Channels
      Mono = 1
      Stereo = 2
    End Enum
    
    Public Sub play(file As String)
      Dim wavefile
      wavefile = file
      Call sndPlaySound(wavefile, SND_ASYNC Or SND_FILENAME)
    End Sub
    
    
    Public Sub StartRecord(ByVal BPS As BitsPerSec, _
      ByVal SPS As SampelsPerSec, ByVal Mode As Channels)
    
      Dim retStr As String
      Dim cBack As Long
      Dim BytesPerSec As Long
    
      retStr = Space$(128)
      BytesPerSec = (Mode * BPS * SPS) / 8
      mciSendString "open new type waveaudio alias capture", retStr, 128, cBack
      mciSendString "set capture time format milliseconds" & _
        " bitspersample " & CStr(BPS) & _
        " samplespersec " & CStr(SPS) & _
        " channels " & CStr(Mode) & _
        " bytespersec " & CStr(BytesPerSec) & _
        " alignment 4", retStr, 128, cBack
      mciSendString "record capture", retStr, 128, cBack
    End Sub
    
    Public Sub SaveRecord(strFile)
      Dim retStr As String
      Dim TempName As String
      Dim cBack As Long
      Dim fs, F
    
      TempName = strFile 'Left$(strFile, 3) & "Temp.wav"
      retStr = Space$(128)
      mciSendString "stop capture", retStr, 128, cBack
      mciSendString "save capture " & TempName, retStr, 128, cBack
      mciSendString "close capture", retStr, 128, cBack
    End Sub
    

    Then you could use it like this:

    Private Sub StartRecord_Click()
      sndmodule.StartRecord Bits16, Sampels32000, Mono
    End Sub
    
    Private Sub EndRecord_Click()
      sndmodule.SaveRecord "C:\Users\Johanness\Downloads\test.wav"
    End Sub
    
    Private Sub Play_Click()
      sndmodule.play "C:\Users\Johanness\Downloads\test.wav"
    End Sub
    

    Thursday, August 27, 2015 5:39 AM
  • I did what you suggested. I can record and stop recording. But when I play nothing happens. I can see a wave file in the directory I set it to go to (C:\Temp) but it will not play.

    For reference I am using Windows 10.


    Steve C. Leigh

    Thursday, August 27, 2015 1:22 PM
  • This forum is for discussing about Excel for Developers, because your issue is about VBA that is outside the scope of this forum.
    So I will suggest you confirming this issue on the VBA forum.

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=isvvba
    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us.
    Thank you for your understanding.

    Friday, August 28, 2015 9:17 AM