0

我正在与 NAudio 合作,将音频文件 .wav 准确地从一台电脑发送到另一台电脑。

我已尝试通过网络流发送消息,但我无法检查消息是否已正确发送,因为到目前为止我在接收代码时遇到问题。

这是发送代码。

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();
    }

这是接收代码

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.

4

1 回答 1

1

您不能将 WaveFileWriter 与 NetworkStream 一起使用,因为 WAV 文件头包含在整个文件被写入之前未知的长度信息。所以标题是最后写入的,需要一个可搜索的流。

无需流式传输 WAV 文件,只需发送 PCM 音频(如果需要,请先发送格式信息)并将其放入另一端的 WAV 文件中。

于 2013-07-04T17:31:44.707 回答