Solved

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

Posted on 2008-10-30
6
1,863 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:Dov_B
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 22845915
There's plenty of free applications to download that will do that...even one or two, I think, that come with Windows.
0
 
LVL 3

Expert Comment

by:Diveblue
ID: 22846002
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
0
 

Author Comment

by:Dov_B
ID: 22846354
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Accepted Solution

by:
Diveblue earned 500 total points
ID: 22850118
the pinvoke signature was a bit off....here's a revised (tested) version...

Imports Microsoft.VisualBasic.Devices
Imports Microsoft.VisualBasic
Imports System.Runtime.InteropServices


Public Class Form1

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


    Public Sub Record()
        Dim result As Long
        Try
            result = mciSendString("open new Type waveaudio Alias recsound", "", 0, Nothing)

            result = mciSendString("record recsound", Nothing, 0, Nothing)
        Catch ex As Exception

        End Try

    End Sub
    Public Sub StopAndSave()
        Dim result As Long
        result = mciSendString("save recsound c:\record.wav", Nothing, 0, Nothing)
        result = mciSendString("close recsound ", Nothing, 0, Nothing)
        My.Computer.Audio.Stop()

    End Sub

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

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

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

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

Author Comment

by:Dov_B
ID: 22863265
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

0
 

Author Comment

by:Dov_B
ID: 22863278
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
A short film showing how OnPage and Connectwise integration works.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

914 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

20 Experts available now in Live!

Get 1:1 Help Now