Solved

recording a sound file

Posted on 1998-11-16
10
152 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month9 days, 10 hours left to enroll

624 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