Solved

recording a sound file

Posted on 1998-11-16
10
151 Views
Last Modified: 2010-04-30
Hello can someone please show me how to use the multimedia control to record a voice file and save it to the hard drive.
If there is a better way to do this without the multimedia control then I'm all ears.

Thank You.
0
Comment
Question by:cavacasp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 3

Expert Comment

by:HATCHET
ID: 1445020
1)  Create a new project and add a Form and a Module to it.

2)  Create the following 4 buttons on your main form :

cmdRecord
cmdStop
cmdPlay
cmdClose

3)  Put this in the Form :

Option Explicit

Private Sub cmdClose_Click()

  Unload Me
  End

End Sub

Private Sub cmdPlay_Click()
   
  PlayRecSound
 
End Sub

Private Sub cmdRecord_Click()
 
  RecordSound
 
End Sub

Private Sub cmdStop_Click()

  CloseSound

End Sub

Private Sub Form_Unload(Cancel As Integer)
 
  CloseSound
 
End Sub

4)  Put this in a Module :

Option Explicit

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 Declare Function mciGetErrorString Lib "WinMM.dll" Alias "mciGetErrorStringA" (ByVal dwError As Long, ByVal lpstrBuffer As String, ByVal uLength As Long) As Long

'Close the sound file
Public Function CloseSound()
On Error GoTo ErrorTrap
 
  Dim Result As Long
  Dim ErrorMsg As Integer
  Dim ReturnString As String * 1024
  Dim ErrorString As String * 1024

  Result = mciSendString("close mysound", ReturnString, 1024, 0)
 
  Exit Function
 
ErrorTrap:
  If Err.Number = 0 Then
    Resume Next
  ElseIf Err.Number = 20 Then
    Resume Next
  Else
    MsgBox Err.Source & " caused an error while closing sound." & Chr(13) & Chr(13) & "Error Number = " & Err.Number & Chr(13) & "Error Description = " & Err.Description & Chr(13), vbOKOnly + vbExclamation, "  RecordWAV - Error"
    Exit Function
  End If

End Function

'Plays the recoreded sound aliased by mysound
Public Function PlayRecSound()
On Error GoTo ErrorTrap

  Dim Result As Long
  Dim ErrorMsg As Integer
  Dim ReturnString As String * 1024
  Dim ErrorString As String * 1024

  Result = mciSendString("stop mysound", ReturnString, 1024, 0)
  If Not Result = 0 Then
    ErrorMsg = mciGetErrorString(Result&, ErrorString, 1024)
    MsgBox ErrorString, vbOKOnly + vbExclamation, "  Play Recorded Sound - Error"
  End If
 
  Result = mciSendString("play mysound from 1 wait", ReturnString, 1024, 0)
  If Not Result = 0 Then
    ErrorMsg = mciGetErrorString(Result&, ErrorString, 1024)
    MsgBox ErrorString, vbOKOnly + vbExclamation, "  Play Recorded Sound - Error"
  End If
   
  Exit Function
 
ErrorTrap:
  If Err.Number = 0 Then
    Resume Next
  ElseIf Err.Number = 20 Then
    Resume Next
  Else
    MsgBox Err.Source & " caused an error while playing sound." & Chr(13) & Chr(13) & "Error Number = " & Err.Number & Chr(13) & "Error Description = " & Err.Description & Chr(13), vbOKOnly + vbExclamation, "  RecordWAV - Error"
    Exit Function
  End If
   
End Function

'Records sound aliased as "mysound" to memory for six seconds
Public Function RecordSound()
On Error GoTo ErrorTrap

  Dim Result As Long
  Dim ErrorMsg As Integer
  Dim ReturnString As String * 1024
  Dim ErrorString As String * 1024
 
  CloseSound
 
  Result = mciSendString("open new type waveaudio alias mysound", ReturnString, 1024, 0)
  If Not Result = 0 Then
    ErrorMsg = mciGetErrorString(Result&, ErrorString, 1024)
    MsgBox ErrorString, vbOKOnly + vbExclamation, "  Play Recorded Sound - Error"
    Exit Function
  End If
 
  'The following command sets the time format to milliseconds
  'and sets the waveform audio format to 8 bit, mono, 11 kHz:
  Result = mciSendString("set mysound time format ms bitspersample 8 samplespersec 11025", ReturnString, 1024, 0)
  If Not Result = 0 Then
    ErrorMsg = mciGetErrorString(Result&, ErrorString, 1024)
    MsgBox ErrorString, vbOKOnly + vbExclamation, "  Play Recorded Sound - Error"
    Exit Function
  End If
 
  'Record for 6000 milliseconds
  Result = mciSendString("record mysound to 6000", ReturnString, 1024, 0)
  If Not Result = 0 Then
    ErrorMsg = mciGetErrorString(Result&, ErrorString, 1024)
    MsgBox ErrorString, vbOKOnly + vbExclamation, "  Play Recorded Sound - Error"
    Exit Function
  End If

  Exit Function
 
ErrorTrap:
  If Err.Number = 0 Then
    Resume Next
  ElseIf Err.Number = 20 Then
    Resume Next
  Else
    MsgBox Err.Source & " caused an error while recording sound." & Chr(13) & Chr(13) & "Error Number = " & Err.Number & Chr(13) & "Error Description = " & Err.Description & Chr(13), vbOKOnly + vbExclamation, "  RecordWAV - Error"
    Exit Function
  End If

End Function

_______________________________________________________

Lemme know whatcha think.

HATCHET
0
 

Author Comment

by:cavacasp
ID: 1445021
Does this only record for 6 seconds that's not good I need to record for a variable length of time.  Until they hit the stop button.
0
 

Author Comment

by:cavacasp
ID: 1445022
Sorry I need to reject this because I need an answer.  If you can answer the question above then I'll be happy to reward you with the points.
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 3

Expert Comment

by:HATCHET
ID: 1445023
cavacasp,

You're kidding right?!  Dude.. if you need a variable length of time to record, MAKE UP A WAY TO DO IT!  It's your program!  Your question doesn't say "Someone write my program for me".  It says "recording a sound file".  I've given you a way to record sounds using the Windows API (the best way to do it).  I can give you the way to do it with the MCI Control if you need it, but I did what you said above.  The purpose of Experts-Exchange is to get you headed in the right direction.  I've done just that and I'd appreciate some points for my effort.

HATCHET
0
 

Author Comment

by:cavacasp
ID: 1445024
Recording a 6 second sound file does nothing for me.  I need to record a file.  Not be restricted to only being able to record 6 seconds or some predetermined length of time.
0
 
LVL 2

Expert Comment

by:trillo
ID: 1445025
Thath's IMPOSSIBLE
Why?.... Because in the background, what you're doing is sending buffers to be filled by the soundcard, and you can obviously not send an infinite buffer of bytes....You have two chices, 1) take HATCHET's code or 2) Move to the low level API. then you can maybe control better what you want, but this is naturally very difficult. Actually I'm developing an app that records sound using the low level API but I'm using VC++, y don't really know if it's possible with VB.
You have to think that even the Win95 Soundrecorder has a limited time to record.
0
 

Author Comment

by:cavacasp
ID: 1445026
I technically mean I need to record for an infinite number of seconds.  But I can't have a fixed length of time that each recording is.  I don't want 4 secs. of dead time if they only record 2 secs and the could need to record say about 20 secs, so if I set the time to 20 secs there would be 18 secs of dead time which is obviously not acceptable.
0
 
LVL 2

Accepted Solution

by:
trillo earned 90 total points
ID: 1445027
You can try this simple code (It's almost something like above)
You can set the recording interval to 20000 miliseconds (in this example) or any reasonable number).... As you can see, the record command does not use the wait flag, so if the user clicks the Stop button, the recorder will stop. If you save the recording to a file, you'll be saving only the time recorded (not the actual 20-or-more seconds).

'---------------------------------------
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
Private Declare Function mciGetErrorString Lib "WinMM.dll" Alias "mciGetErrorStringA" (ByVal dwError As Long, ByVal lpstrBuffer As String, _
       ByVal uLength As Long) As Long

Dim i As Long

Private Sub cmdPlay_Click()
    i = mciSendString("play anyname from 1 wait", 0, 0, 0)
End Sub

Private Sub cmdRec_Click()
    i = mciSendString("record anyname to 20000", 0, 0, 0)
End Sub

Private Sub cmdStop_Click()
    i = mciSendString("stop anyname", 0, 0, 0)
End Sub

Private Sub Form_Load()
    i = mciSendString("open new type waveaudio alias anyname", 0, 0, 0)
End Sub

Private Sub Form_Unload(Cancel As Integer)
    i = mciSendString("close anyname", 0, 0, 0)
End Sub
'---------------------------------------

As you can see, you also don't need to send every time a string buffer in the function (only when you want to retrieve information about a device).

Trillo
0
 

Author Comment

by:cavacasp
ID: 1445028
Thank You that's is wait I was looking for.
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1445029
Bought This Question.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question