Link to home
Start Free TrialLog in
Avatar of Dov_B
Dov_B

asked on

is there some short code to record audio from microphone save as mp3 or wma in vb.net

I am real new to vb.net does anyone have a code snippet to record audio from the microphone and save as mp3 or wma
Avatar of David L. Hansen
David L. Hansen
Flag of United States of America image

There's plenty of free applications to download that will do that...even one or two, I think, that come with Windows.
Avatar of Diveblue
Diveblue

try this.....should create a wave file....mp3 would require an encoder

  <DllImport("winmm.dll", EntryPoint:="mciSendStringA", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
        Public Shared Sub mciSendString(ByVal cmd As String, ByRef returnMsg As String, ByRef RetStringLen As Long, ByRef Hwnd As Long)
    End Sub


    Public Sub Record()
        mciSendString("open new Type waveaudio Alias recsound", "", 0, 0)
        mciSendString("record recsound", "", 0, 0)

    End Sub
    Public Sub StopAndSave()
        mciSendString("save recsound c:\record.wav", "", 0, 0)
        mciSendString("close recsound ", "", 0, 0)
        My.Computer.Audio.Stop()

    End Sub

    Public Sub Play()
        My.Computer.Audio.Play("c:\record.wav", AudioPlayMode.Background)
    End Sub
Avatar of Dov_B

ASKER

thanx for the code I am working with vb.net 2008 express
your code had a few things vb 2008 wanted changed which I did but when I clicked the button that ran the record subroutine a second later the application just aborted without giving any reason
the changes vb gave were

Imports System.Runtime.InteropServices
and

<Runtime.InteropServices.DllImport
is it possible for you to fix it thankyou for the code
ASKER CERTIFIED SOLUTION
Avatar of Diveblue
Diveblue

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dov_B

ASKER

Thanx for your code I later found code that will encode as wma I have added it here
Imports WMEncoderLib
Imports WMPREVIEWLib
 
Public Class WinMediaEncoding
    Private _Encoder As New WMEncoder
    Private _strFileName As String
 
    Public ReadOnly Property EncodingTime() As Decimal
        Get
            Return _Encoder.Statistics.EncodingTime
        End Get
    End Property
 
    Public ReadOnly Property FileName() As String
        Get
            Return _strFileName
        End Get
    End Property
 
 
    Public Function RecordWma(ByVal OutPutFilePath As String, Optional ByVal ProfileName As String = "") As Boolean
        Try
            _Encoder.Reset()
            If Trim(ProfileName) = "" Then
                ProfileName = "Windows Media Audio 8 for Dial-up Modem (FM Radio Stereo, 28.8 Kbps)"
            End If
 
            Dim SrcGrp As IWMEncSourceGroup2
            Dim SrcGrpColl As IWMEncSourceGroupCollection
 
            SrcGrpColl = _Encoder.SourceGroupCollection
            SrcGrp = CType(SrcGrpColl.Add("SG_1"), IWMEncSourceGroup2)
 
            Dim SrcAud As IWMEncAudioSource
            SrcAud = CType(SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO), IWMEncAudioSource)
            SrcAud.SetInput("Default_Audio_Device", "Device", "")
 
            For j As Integer = 0 To _Encoder.ProfileCollection.Count
                If _Encoder.ProfileCollection.Item(j).Name = ProfileName Then
                    SrcGrp.Profile = _Encoder.ProfileCollection.Item(j)
                    Exit For
                End If
            Next
 
            Dim Descr As IWMEncDisplayInfo
 
            Descr = _Encoder.DisplayInfo
            Descr.Author = "abc Co Inc."
            'Descr.Copyright = "Copyright information"
            Descr.Description = "abc desc"
            'Descr.Rating = "Rating information"
            Descr.Title = "abc title"
            Dim Attr As IWMEncAttributes
 
            Attr = _Encoder.Attributes
            Attr.Add("URL", "www.abc.com")
            Dim file As IWMEncFile
 
            file = _Encoder.File
            file.LocalFileName = OutPutFilePath
            _Encoder.Start()
 
            _strFileName = OutPutFilePath
            Return True
        Catch ex As Exception
            Throw ex
        End Try
    End Function
 
    Public Function PauseEncoder() As Boolean
        Try
            If _Encoder.RunState = WMENC_ENCODER_STATE.WMENC_ENCODER_RUNNING Then
                _Encoder.Pause()
            End If
 
        Catch ex As Exception
            Throw ex
        End Try
    End Function
 
    Public Function ResumeEncoder() As Boolean
        Try
            If _Encoder.RunState = WMENC_ENCODER_STATE.WMENC_ENCODER_PAUSED Then
                _Encoder.Start()
            End If
        Catch ex As Exception
            Throw ex
        End Try
        
    End Function
 
    Public Function StopEncoder() As Boolean
        Try
            If _Encoder.RunState <> WMENC_ENCODER_STATE.WMENC_ENCODER_STOPPED Then
                _Encoder.Stop()
            End If
        Catch ex As Exception
            Throw ex
        End Try
 
    End Function
End Class 

Open in new window

Avatar of Dov_B

ASKER

also the way to imports in the above code is to copy wmenc.exe and wmprevu.dll
into the bin folder right click the solution and add reference to these 2 files