Search code examples
c#voip.net-4.0naudio

Attempt of VOIP Server and Client Gone Wrong


Hi I am creating a VOIP client and server, and it is going better than I expected since I was thinking that I was not going to hear nothing for my first attempt. But actually I am hearing myself fine for about a second or two until my program throws an exception within a class I created to simplify the socket process of sending audio bytes and playing them in real time. Here is the code below:

        // public function member of class DataArgs 
        public void PlayRealTimeAudio()
        {

                WaveOut o = new WaveOut();
                o.DeviceNumber = 0;
                o.DesiredLatency = 500;
                Stream s = new MemoryStream(this.Buffer);
                Raw.RawSourceWaveStream raw = new Raw.RawSourceWaveStream(s, new WaveFormat(16000, 2));
                o.Init(raw); // Exception throws here after hearing my voice for a brief moment
                o.Play();
                o.Stop();
                o.Dispose();

            return;
        }


        // Within the Form1 class
        void listener_Streaming(System.Net.Sockets.TcpClient sender, DataArgs e)
        {
            byte[] buf = new byte[e.Length];
            buf = e.Buffer;
            e.PlayRealTimeAudio();
        }

P.S If you need me to show more code please ask, I will be glad to show you.


Solution

  • rather than constantly opening and closing the WaveOut device, the normal way to do this would be to have a single WaveOut device constantly playing and getting its data from a BufferedWaveProvider (available in the latest NAudio code). Then, in listener_streaming, you would simply add the bytes received to the BufferedWaveProvider