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,949 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exit the loop 4 49
Help with error when uploading excel file 3 39
vb.net 1 month apart 11 35
Need help converting bitmap to image in VB.Net 8 30
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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