Click here to Skip to main content
15,888,255 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 ??


C#
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();
            obj.function();
            
        }
        void function()
        {
            Thread t = new Thread(new ThreadStart(Service));
            t.IsBackground = true;
            t.Start();
            //Thread t2 = new Thread(new ThreadStart(Service2));
            //t2.Start();


        }

            void Service()
            {
                sourcestream = new NAudio.Wave.WaveIn();
                
                while(true)
                {
            
            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 )
            {
                sourcestream.StartRecording();
                byte[] buffer = e.Buffer;
                Console.WriteLine(buffer);               

            }
        }
    }
Posted
v2
Comments
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
sourcestream 


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

and other setting

delete while loop

put this to thread's
Service
function
sourcestream.StartRecording();
 
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