Click here to Skip to main content
15,795,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys

I am using the NAudio library to transfer voice over the network i.e. from my microphone to any other remote PC speakers for that i need some help i have used the Naudio library to record a voice from microphone and store it but now when i am trying to send it over the network i am having problems. I am using the following Code(Sender side) but it give me an Exception that Wavein should record in background thread why is this so ??

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication2
    class Program
        private NAudio.Wave.WaveIn sourcestream = null;
        private NAudio.Wave.DirectSoundOut waveOut = null;
        private NAudio.Wave.WaveFileWriter Wavewriter = null;
        static void Main(string[] args)
            Program obj = new Program();
        void function()
            Thread t = new Thread(new ThreadStart(Service));
            t.IsBackground = true;
            //Thread t2 = new Thread(new ThreadStart(Service2));


            void Service()
                sourcestream = new NAudio.Wave.WaveIn();
            int devicenumber = 0;
            sourcestream.DeviceNumber = devicenumber;

            sourcestream.WaveFormat = new NAudio.Wave.WaveFormat(44100, NAudio.Wave.WaveIn.GetCapabilities(devicenumber).Channels);
            sourcestream.DataAvailable += new EventHandler<naudio.wave.waveineventargs>(sourcestream_DataAvailable);
            //Wavewriter = new NAudio.Wave.WaveFileWriter(save.FileName,sourcestream.WaveFormat);

            //void Service2 { 

            private void sourcestream_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e )
                byte[] buffer = e.Buffer;

Member 13001256 15-Feb-17 4:15am    
Did you solve your problem?
I have the same problem just like you.
If you solved the problem, please help me.

1 solution

add private variable for

put init block to Main function
sourcestream = new NAudio.Wave.WaveIn();

and other setting

delete while loop

put this to thread's
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900