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,835 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
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!

 
LVL 3

Accepted Solution

by:
Diveblue earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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 …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

15 Experts available now in Live!

Get 1:1 Help Now