Solved

recording a sound file

Posted on 1998-11-16
10
144 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
  • 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now