Win Coder
Win Coder

Reputation: 6756

Sending .Wav file over the network using NAudio

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.

Upvotes: 0

Views: 2275

Answers (1)

Mark Heath
Mark Heath

Reputation: 49482

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.

Upvotes: 1

Related Questions