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,899 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

773 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