Make sure in the NuPackage manager you run the “Install-Package CSCore” command.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
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 |