Make sure in the NuPackage manager you run the “Install-Package CSCore” command.

Imports CSCore
Imports CSCore.SoundIn
Imports CSCore.Codecs.WAV
''Install-Package CSCore
Module Module1
    WithEvents MyCapture As New WasapiLoopbackCapture()
    Dim MyWaveWriter As WaveWriter
    Sub Main()
        ''Debug.WriteLine(Environment.UserName)
        Dim FilePathToRecordTo As String = System.AppDomain.CurrentDomain.BaseDirectory & Environment.UserName & "-" & Date.Now.ToString("d-M-y-HHmm") & ".mp3"
        MyCapture.Initialize()
        MyWaveWriter = New WaveWriter(FilePathToRecordTo, MyCapture.WaveFormat)
        MyCapture.Start()
        Console.WriteLine(FilePathToRecordTo)
        Console.WriteLine("Press any key to stop the recording")
        Console.ReadKey()
        MyCapture.[Stop]()
    End Sub

    Private Sub MyCapture_DataAvailable(sender As Object, e As DataAvailableEventArgs) Handles MyCapture.DataAvailable
        Console.Write(".")
        MyWaveWriter.Write(e.Data, e.Offset, e.ByteCount)
    End Sub
End Module

and with Naudio for MP3 compression (Better in my opinion)

Imports System
Imports NAudio.lame
Imports NAudio.Wave

''NU Package Managet Console
''install-package NAudio
''install-package NAudio.lame


Module Module1
    Dim wri As LameMP3FileWriter
    Dim stopped As Boolean = False
    WithEvents waveIn As IWaveIn = New WasapiLoopbackCapture()
    Sub Main(ByVal args() As String)
        Dim FilePathToRecordTo As String = System.AppDomain.CurrentDomain.BaseDirectory & Environment.UserName & "-" & Date.Now.ToString("d-m-y-HHMM") & ".mp3"
        wri = New LameMP3FileWriter(FilePathToRecordTo, waveIn.WaveFormat, 32)
        waveIn.StartRecording()
        stopped = False

        Console.WriteLine(FilePathToRecordTo)
        Console.WriteLine("Press ESC key to stop the recording")

        While Not stopped

            If Console.KeyAvailable Then
                Dim key = Console.ReadKey(True)
                If Not key = Nothing AndAlso key.Key = ConsoleKey.Escape Then
                    waveIn.StopRecording()
                End If
            Else
                System.Threading.Thread.Sleep(50)
            End If
        End While

        wri.Flush()
        waveIn.Dispose()
        wri.Dispose()
    End Sub

    Private Sub waveIn_DataAvailable(sender As Object, e As WaveInEventArgs) Handles waveIn.DataAvailable
        If wri IsNot Nothing Then wri.Write(e.Buffer, 0, e.BytesRecorded)
        Console.Write(".")
    End Sub

    Private Sub waveIn_RecordingStopped(sender As Object, e As StoppedEventArgs) Handles waveIn.RecordingStopped
        stopped = True
    End Sub
End Module

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax