如何使用NAudio将WaveStream转换为字节数组?
答案 0 :(得分:2)
public static class StreamExtension
{
public static byte[] ToArray(this Stream stream)
{
byte[] buffer = new byte[4096];
int reader = 0;
MemoryStream memoryStream = new MemoryStream();
while ((reader = stream.Read(buffer, 0, buffer.Length)) != 0)
memoryStream.Write(buffer, 0, reader);
return memoryStream.ToArray();
}
}
答案 1 :(得分:1)
你可以试试这个愿景,应该适合你,试试吧
MemoryStream memoryStr = new MemoryStream();
while ((read = stream.Read(buffer, 0, buffer.Length)) != 0)
memoryStr.Write(buffer, 0, read );
答案 2 :(得分:0)
您可以像System.IO.Stream
一样使用它。
使用Read方法。但要记住。如果您将整个流转换为一个Bytearray
,则可能导致OutOfMemoryException
。这取决于流的大小。但你可以使用这样的东西:
byte[] buffer = new byte[stream.Length];
int read = stream.Read(buffer, 0, buffer.Length);