System sound recorder using NAudio dll with complete code

System sound recorder including microphone with vb.net complete code 

Please Like & Share




code from here 
************************
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports NAudio.Wave
Imports NAudio.CoreAudioApi
Imports System.IO
Imports System.Diagnostics
Imports System.ComponentModel.Composition

Public Class Form1
    Dim Time As New DateTime
    Private waveIn As IWaveIn
    Private writer As WaveFileWriter
    Private outputFilename As String
    Private outputFolder As String
    Public Sub New()
        InitializeComponent()
        If Environment.OSVersion.Version.Major >= 6 Then
            LoadWasapiDevicesCombo()
        Else
            ComboBox1.Enabled = False
            radioButtonWasapiLoopback.Enabled = False
        End If

        outputFolder = Application.StartupPath & "\Temp"
        Directory.CreateDirectory(outputFolder)
    End Sub
    Private Sub LoadWasapiDevicesCombo()
        Dim deviceEnum = New MMDeviceEnumerator()
        Dim devices = deviceEnum.EnumerateAudioEndPoints(DataFlow.All, DeviceState.Active).ToList()
        ComboBox1.DataSource = devices
        ComboBox1.DisplayMember = "FriendlyName"
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim Difference As TimeSpan = DateTime.Now.Subtract(Time)
        Label2.Text = "Time : " & Difference.Hours.ToString & ":" & Difference.Minutes.ToString & ":" & Difference.Seconds.ToString
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Time = DateTime.Now
        Timer1.Start()
        Button1.Enabled = False
        Button2.Enabled = True
        If radioButtonWaveIn.Checked Then Cleanup()

        If waveIn Is Nothing Then
            CreateWaveInDevice()
        End If
        outputFilename = String.Format("programmingABC DEmo{0:yyy-mm-dd hh-mm-ss}.wav", DateTime.Now)
        writer = New WaveFileWriter(Path.Combine(outputFolder, outputFilename), waveIn.WaveFormat)
        waveIn.StartRecording()
    End Sub
    Private Sub CreateWaveInDevice()
        If radioButtonWaveIn.Checked Then
            waveIn = New WaveIn()
            waveIn.WaveFormat = New WaveFormat(8000, 1)
        Else
            waveIn = New WasapiLoopbackCapture()
        End If
        AddHandler waveIn.DataAvailable, AddressOf OnDataAvailable
        AddHandler waveIn.RecordingStopped, AddressOf OnRecordingStopped
    End Sub

    Private Sub OnRecordingStopped(ByVal sender As Object, ByVal e As StoppedEventArgs)
        If InvokeRequired Then
            BeginInvoke(New EventHandler(Of StoppedEventArgs)(AddressOf OnRecordingStopped), sender, e)
        Else
            FinalizeWaveFile()

            If e.Exception IsNot Nothing Then
                MessageBox.Show(String.Format("Error{0}", e.Exception.Message))
            End If
        End If
    End Sub
    Private Sub Cleanup()
        If waveIn IsNot Nothing Then
            waveIn.Dispose()
            waveIn = Nothing
        End If
        FinalizeWaveFile()
    End Sub
    Private Sub FinalizeWaveFile()
        If writer IsNot Nothing Then
            writer.Dispose()
            writer = Nothing
        End If
    End Sub
    Private Sub OnDataAvailable(ByVal sender As Object, ByVal e As WaveInEventArgs)
        If Me.InvokeRequired Then
            Me.BeginInvoke(New EventHandler(Of WaveInEventArgs)(AddressOf OnDataAvailable), sender, e)
        Else
            writer.Write(e.Buffer, 0, e.BytesRecorded)
            Dim SecondsRecorded As Integer = CInt((writer.Length / writer.WaveFormat.AverageBytesPerSecond))

        End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Timer1.Stop()
        Button1.Enabled = True
        If waveIn IsNot Nothing Then
            waveIn.StopRecording()
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Application.Exit()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button2.Enabled = False
    End Sub
End Class
 

Comments

Popular posts from this blog

Complete Python Database Project with sqlite3

Complete StopWatch in Java with source code