0

I am working with NAudio to send audio files .wav to be exact from one pc to another.

I have tried sending the message over the network stream but i have no way of checking whether the message had been sent correctly or not because so far i am having problems with receiving the code.

Here's the sending code.

public void StartConnection()
    {
        _connection = new TcpClient("localhost",1111);
        _stream = _connection.GetStream();
        SendFile(_stream,_waveStream);
    }

public void SendFile(NetworkStream StreamToWrite,WaveStream StreamToSend)
    {
        WaveFileWriter write = new WaveFileWriter(StreamToWrite,StreamToSend.WaveFormat);
        byte[] decoded = FromStreamToByte(StreamToSend);
        write.Write(decoded,0,decoded.Length);
        write.Flush();
    }

and here is the receiving code

public void ListenConnection()
    {
        _listener = new TcpListener(IPAddress.Any,1111);
        _listener.Start();
        TcpClient receiver = _listener.AcceptTcpClient();
        _stream = receiver.GetStream();
    }

public void ReadFile(NetworkStream stream)
    {
        WaveFileReader read = new WaveFileReader(stream);
    }

Now i am having trouble on where to continue with receiving code because if i call read method of read then it asks for a byte array, offset and length. But why it asks for an array is beyond me after all its just receiving data.

Any advice on how should i proceed further with the ReadFile method.

UPDATE---

During Debugging i found out that NetworkStream that was being passed to SendFile for use in WaveFileWriter has not determined length and so it gives the Stream Does not Support Seek Operations. However i don't understand why it gives this error because its prototype says it can accept any Stream.

Win Coder
  • 6,628
  • 11
  • 54
  • 81

1 Answers1

1

You can't use WaveFileWriter with a NetworkStream, because the WAV file header contains length information that is not known until the whole file has been written. So the header is written last, requiring a seekable stream.

Instead of streaming a WAV file, just send the PCM audio (and format information first if you need) and put it into a WAV file at the other end.

Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • Now that i notice my question title is a bit misleading because i am not exactly sending a .wav file over networkstream. Instead the `StreamToSend` in SendFile is just yet another stream that i obtained following [this](http://opensebj.blogspot.com/2009/02/introduction-to-using-naudio.html) tutorial. – Win Coder Jul 04 '13 at 17:40
  • If you've already got a WAV fike, just send it over a NetworkStream. No need to involve WaveFileWriter at all. – Mark Heath Jul 05 '13 at 06:46
  • Hi @MarkHeath Could you give us an example of how to send data over the network captured by WasapiLoopbackCapture? How can I send the PCM audio? Thanks! – Fernando Pelliccioni Dec 26 '13 at 19:33