locked
Recording sound from microphone in VB 2005 EE RRS feed

  • Question

  • Please, could you  tell me how to record sound from microphone in VB 2005 EE?. If you would be so kind anything simply.

    Thanks.

    Saturday, February 17, 2007 8:33 AM

Answers

  • Public Class Form1

    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' record from microphone

    mciSendString("open new Type waveaudio Alias recsound", "", 0, 0)

    mciSendString("record recsound", "", 0, 0)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    ' stop  and save

    mciSendString("save recsound c:\record.wav", "", 0, 0)

    mciSendString("close recsound", "", 0, 0)

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    ' play audio

    My.Computer.Audio.Play("c:\record.wav", AudioPlayMode.Background)

    End Sub

    End Class

    Saturday, February 24, 2007 10:37 PM

All replies

  • cunis ta tu agus cead mile failte
    Saturday, February 17, 2007 11:12 AM
  • I don't understand, in spite of this thank you.
    Saturday, February 17, 2007 2:59 PM
  •  

    These fora only speak English.

    Saturday, February 17, 2007 4:26 PM
  • Is it possible at all?

     

    Saturday, February 17, 2007 5:04 PM
  • Is there any sample?
    Monday, February 19, 2007 10:09 AM
  • sorry bubo

    I wish I could help but the only way I know to do that would be to open another program...like super mp3 recorder

    i.e

    process.start.smrpro.exe

    the help is not very good on here lately. and I am only learning......

    maybe you should re post your question........and then just maybe someone might help

    sorry

    Monday, February 19, 2007 11:22 AM
  • That's allright, cheyenne.

    I want to record sound in my programme.

    Thank you.

    Monday, February 19, 2007 11:46 AM
  • Try DirectX SDK.
    Wednesday, February 21, 2007 8:53 AM
  • Thank you, but it seems too dificult for me.

     

    Thursday, February 22, 2007 4:25 PM
  • You might try this site. I found it to be easy to follow.

    http://www.geocities.com/smigman.geo/mci/wav.html

    I may be working on the same goal soon, I'll check back to see if this helps for you.

    Sam
    Thursday, February 22, 2007 7:53 PM
  • Thank you very much!

     I I tried it but this program for Visual Basic 4.0. It doesn't work in Visual Basic 2005 Express Edition.

    I am much obliged.

    Bubo 

     

    Thursday, February 22, 2007 8:34 PM
  • Public Class Form1

    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' record from microphone

    mciSendString("open new Type waveaudio Alias recsound", "", 0, 0)

    mciSendString("record recsound", "", 0, 0)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    ' stop  and save

    mciSendString("save recsound c:\record.wav", "", 0, 0)

    mciSendString("close recsound", "", 0, 0)

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    ' play audio

    My.Computer.Audio.Play("c:\record.wav", AudioPlayMode.Background)

    End Sub

    End Class

    Saturday, February 24, 2007 10:37 PM
  • It's beautiful. It works.

    Thanks, Bubo.

    Sunday, February 25, 2007 8:50 AM
  • Wow, thank you!!! I was looking for a solution like this for a week now! (I was trying to work with overcomplicated sound class libraries that are not even implemented in VB.NET to do exactly what this code does until I stumbled across this thread)

    I have a couple of questions:

    Is it possible to record the sound and then play it without having to store the file in the harddrive?

    If it's not possible, then is there a way to delete the record.wav file after playing it?

    Thanks

     

     

    Sunday, March 4, 2007 12:16 AM
  • Nevermind about my questions, I just found some codes that deletes the file out of C drive

    Thanks for the recording and playing code! It's really awesome!

    Sunday, March 4, 2007 12:29 AM
  • kill " c:\record.wav"
    Sunday, March 4, 2007 9:13 PM
  • hi blader

    great piece of code......thank you

    could you tell me .... how can I save recording without overwriting existing recording

    I need a ....save as    or something.......thanks a million

    clint

    Thursday, March 8, 2007 8:40 AM
  • Try adding a save file dialog and changing the code for button 2 as follows

    The savefiledialog will kick up a fuss if the filename already exists

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    ' stop and save

    SaveFileDialog1.InitialDirectory = "c:\"

    SaveFileDialog1.DefaultExt = ".wav"

    SaveFileDialog1.Filter = "Wave Files|*.wav"

    SaveFileDialog1.FilterIndex = 1

    If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

    Dim Filetosave As String = "save recsound " & SaveFileDialog1.FileName

    mciSendString(Filetosave, "", 0, 0)

    Else

    MsgBox("You no Save!!!")

    End If

    mciSendString("close recsound", "", 0, 0)

    End Sub

    Thursday, March 8, 2007 11:52 AM
  • Try This (just add a savefiledialog to your form)

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    ' stop and save

    SaveFileDialog1.InitialDirectory = "c:\"

    SaveFileDialog1.DefaultExt = ".wav"

    SaveFileDialog1.Filter = "Wave Files|*.wav"

    SaveFileDialog1.FilterIndex = 1

    If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

    Dim Filetosave As String = "save recsound " & SaveFileDialog1.FileName

    mciSendString(Filetosave, "", 0, 0)

    Else

    MsgBox("You no Save!!!")

    End If

    mciSendString("close recsound", "", 0, 0)

    End Sub

     

    Thursday, March 8, 2007 11:57 AM
  • Public Class Form1

    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer

    Dim SoundName As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' record from microphone

    mciSendString("open new Type waveaudio Alias recsound", "", 0, 0)

    mciSendString("record recsound", "", 0, 0)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Me.SoundName = InputBox("Your sound name")

    ' stop and save new recording

    mciSendString("save recsound c:\" & Me.SoundName & ".wav", "", 0, 0)

    mciSendString("close recsound", "", 0, 0)

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    ' play audio

    My.Computer.Audio.Play("c:\" & Me.SoundName & ".wav", AudioPlayMode.Background)

    End Sub

     

    End Class

    Thursday, March 8, 2007 5:25 PM
  • OK?
    Thursday, March 8, 2007 5:34 PM
  • You can try this:

    Public Class Form1

    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer

    Dim SoundName As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' record from microphone

    mciSendString("open new Type waveaudio Alias recsound", "", 0, 0)

    mciSendString("record recsound", "", 0, 0)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Me.SoundName = InputBox("Your sound name")

    ' stop and save new recording

    mciSendString("save recsound c:\" & Me.SoundName & ".wav", "", 0, 0)

    mciSendString("close recsound", "", 0, 0)

    Me.ComboBox1.Items.Add(Me.SoundName)

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

    My.Computer.Audio.Play("c:\" & Me.ComboBox1.SelectedItem & ".wav", AudioPlayMode.Background)

    End Sub

    End Class

    Thursday, March 8, 2007 6:23 PM
  • forums were wierd last night, i got double posts as well :(
    Thursday, March 8, 2007 10:17 PM
  • thanks very much guys

    I will play with this later......it looks like just what I need

    thank you

    clint

    Friday, March 9, 2007 6:39 AM
  • hello ,

     I tried this code can you please tell me why it is not working on my computer? do I need to add some reference? Please Help.

    thanks

    Thursday, April 19, 2007 8:24 PM
  • Couple of things from this.

    I am also needing to record files now.  This code worked from the standpoint that it did record, but there is tons of static.  Any suggestions on how to clean it up?

    The other issue is I have to have my recorded files recorded at 16kHz, 16-bit, mono.  Am I able to set any settings with this?  If not, does anyone have any suggestions on how to do this?

     

    Thanks.

    Monday, April 23, 2007 12:55 AM
  • i tried using this code in VB.net 2003  Architect edition and VB.net 2005 Express Edition and was not able to get this to work at all.  Any ideas?

     

     Blader wrote:

    Public Class Form1

    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' record from microphone

    mciSendString("open new Type waveaudio Alias recsound", "", 0, 0)

    mciSendString("record recsound", "", 0, 0)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    ' stop  and save

    mciSendString("save recsound c:\record.wav", "", 0, 0)

    mciSendString("close recsound", "", 0, 0)

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    ' play audio

    My.Computer.Audio.Play("c:\record.wav", AudioPlayMode.Background)

    End Sub

    End Class

    Sunday, May 13, 2007 1:41 AM
  • I (like a few posts above) am trying to change the attributes of the recorded file.  The code is working great, but I need to record the file in 8-bit, CCITT U-law format.  Does this method of recording allow you to change the file attributes?  Sample code would be appreciated if at all possible.  Or, if someone knows another method of recording that will allow changing the file format that would also be appreciated.

     

    Thanx,

    ryan.

    Friday, May 18, 2007 11:06 PM
  • Try importing some namespaces System.Runtime.InteropServices and System.Text. By the way the code was great, also looking for simpler version of recording then I found this, I'm using System.Media to play the sound but I'm planning to use the winmm.dll way. I converted the code to C# since I'm developing my application with it. Thanks...
    Tuesday, June 19, 2007 4:09 AM
  •  Blader wrote:

    Public Class Form1

    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' record from microphone

    mciSendString("open new Type waveaudio Alias recsound", "", 0, 0)

    mciSendString("record recsound", "", 0, 0)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    ' stop  and save

    mciSendString("save recsound c:\record.wav", "", 0, 0)

    mciSendString("close recsound", "", 0, 0)

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    ' play audio

    My.Computer.Audio.Play("c:\record.wav", AudioPlayMode.Background)

    End Sub

    End Class




    this code only record sound from mic not the back sound. is there someone know code for record whole the sound (record sound from mic + sound from windows like mp3 etc) ? I really need it right now thx

    Wednesday, December 12, 2007 5:55 AM
  • hi makado

    this code does record all music, check your pc sound settings

    I record everything, mp3s. internet, mic.....,

    clint 

    Wednesday, December 12, 2007 2:24 PM
  •  clint 2 wrote:

    hi makado

    this code does record all music, check your pc sound settings

    I record everything, mp3s. internet, mic.....,

    clint 



    I don't know why but it really only record sound from mic and I have tried it for many times. is there any suggestion for the pc sound settings ?
    Sunday, December 16, 2007 4:37 PM
  • well if you double click the volume icon in the taskbar

    then click options...properties...recording

    then click ok

    you then choose sterio mixer, and you will then be able to record from pc

    cheers

    clint

    Monday, December 17, 2007 7:05 AM
  •  clint 2 wrote:

    well if you double click the volume icon in the taskbar

    then click options...properties...recording

    then click ok

    you then choose sterio mixer, and you will then be able to record from pc

    cheers

    clint



    I have click options -> properties - > recording then click ok. but i can't find the stereo mixer, all i see is the recording control(like mocrophone, aux, line in, CD player, phone, mono out, wave out mix). is that what you mean?

    i'm sory to bothering you but i really need this code. now i'm doing my thesis and i have a problem with record sound.
    thanks for your help..
    Monday, December 17, 2007 7:51 AM
  • one of them should work....maybe wav out or mono

    Monday, December 17, 2007 8:02 AM
  • clint, now it's works. thanks a lot
    Monday, December 17, 2007 10:42 PM
  • your welcome

    so  glad I could help

    clint

    Tuesday, December 18, 2007 6:41 AM
  • hi....... my name is amar and first of all i thanks for to this it work for recording but i face the problem on this code is playing the recorded there is the problem on this line

    My.Computer.Audio.Play("c:\record.wav", AudioPlayMode.Background)

    i want a help from you.......! please help me on this line i.e on code my.computer........ and my e-mail id is amarec_004@yahoomail.com thankyou.......

    Saturday, March 1, 2008 1:38 PM
  • You would use the Win32 API for winmm.dll. You will be able to record, read the recorded data, and save as a wave file. You are not able to read the data before saving with MCI
    • Proposed as answer by LetsGoNative Sunday, February 8, 2009 12:07 AM
    Friday, January 16, 2009 5:01 PM
  • i whant to record sound from mic to array & edit him
    Thursday, February 19, 2009 6:37 PM
  •  Blader wrote:

    Public Class Form1

    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' record from microphone

    mciSendString("open new Type waveaudio Alias recsound", "", 0, 0)

    mciSendString("record recsound", "", 0, 0)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    ' stop  and save

    mciSendString("save recsound c:\record.wav", "", 0, 0)

    mciSendString("close recsound", "", 0, 0)

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    ' play audio

    My.Computer.Audio.Play("c:\record.wav", AudioPlayMode.Background)

    End Sub

    End Class




    this code only record sound from mic not the back sound. is there someone know code for record whole the sound (record sound from mic + sound from windows like mp3 etc) ? I really need it right now thx


    Hi Blader

    could you please give me a code in continuation to above one with whcih I can  record left and right channels seperately.

     

    regards,

    Ravi

    Thursday, August 27, 2009 8:01 AM
  • Is there any way to record sound  from another source,for example from S/PDIF input?
    Wednesday, February 10, 2010 8:04 AM
  • Thanks, Blader. This was exaclty what I was looking for.
    Sunday, April 18, 2010 2:47 PM
  • Dear Blader,

    Thanks for the code... but I am waiting for your kind response....

    Anyone please provide me your help on saving recorded file to the path other than root of C:\ or D:\ specially when I give path of a folder which has its title with two words (i.e "D:\Recordings\Hello World").  It saves recorded file while I give name without spaces(i.e "D:\Recordings\HelloWorld") but not with spaces (as said before)..

    I would appreciate help from anyone on this...

    Thanks,

     Ravi

     


     

    Thursday, June 30, 2011 9:39 AM
  • Can anyone help me with this problem but in VB for Access.

    I have same problem but my app is in MS Access and I have never work on VB Studio with C++ or else...

    Thanks!!

    Wednesday, March 7, 2012 1:08 AM
  • hi, thanx for the code .. i have been searched a lot for how to save and finally i found it ^__^ BUT .. i have a problem .. the recording continue without stopping .. i tried to change the place of close recsound but it didn't work ..!! Any Help!!!! Regards, Rim
    Tuesday, April 2, 2013 12:03 PM
  • Dear Blader,

    Thanks for the code... but I am waiting for your kind response....

    Anyone please provide me your help on saving recorded file to the path other than root of C:\ or D:\ specially when I give path of a folder which has its title with two words (i.e "D:\Recordings\Hello World").  It saves recorded file while I give name without spaces(i.e "D:\Recordings\HelloWorld") but not with spaces (as said before)..

    I would appreciate help from anyone on this...

    Thanks,

     Ravi

     


     

    Hi,

    In order to save it to a path that has spaces in it you must add quotes to the begining and end of the path and name like this

            Dim saveas As String = Chr(34) & "C:\Folder 1\Folder 2\My Recording.wav" & Chr(34) ' Chr(34) is the ascii code for a quote
            mciSendString("save recsound " & saveas, "", 0, 0)
            mciSendString("close recsound", "", 0, 0)
    

    Wednesday, April 3, 2013 4:19 PM
  • hi, thanx for the code .. i have been searched a lot for how to save and finally i found it ^__^ BUT .. i have a problem .. the recording continue without stopping .. i tried to change the place of close recsound but it didn't work ..!! Any Help!!!! Regards, Rim

    Imports System.Runtime.InteropServices
    Imports System.Text
    
    Public Class Form1
    
        <DllImport("winmm.dll")> _
        Private Shared Function mciSendString(ByVal command As String, ByVal buffer As StringBuilder, ByVal bufferSize As Integer, ByVal hwndCallback As IntPtr) As Integer
        End Function
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
                Dim i As Integer
                i = mciSendString("open new type waveaudio alias capture", Nothing, 0, 0)
                i = mciSendString("record capture", Nothing, 0, 0)
                Label1.Text = "Recording"
                Label1.BackColor = Color.Green
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Try
                Dim i As Integer
                i = mciSendString("save capture " & "C:\Users\John\Desktop\Test.Wav", Nothing, 0, 0)
                i = mciSendString("close capture", Nothing, 0, 0)
                Label1.Text = "Idle"
                Label1.BackColor = Color.Yellow
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
    End Class


    You've taught me everything I know but not everything you know.

    Wednesday, April 3, 2013 4:38 PM