如何将NAudio WaveStream写入内存流?

时间:2012-07-16 07:59:19

标签: c# stream naudio

我有一个程序,它接收一个字节数组中的mp3数据。它必须将mp3数据转换为wav格式并将其存储在字节数据中。我正在尝试将NAudio用于此目的。我为此目的使用以下代码。

Stream inputStream = ...;
Stream outputStream = ...;

using (WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(new Mp3FileReader(inputStream)))
using (WaveFileWriter waveFileWriter = new WaveFileWriter(outputStream, waveStream.WaveFormat))
{
    byte[] bytes = new byte[waveStream.Length];
    waveStream.Read(bytes, 0, waveStream.Length);
    waveFileWriter.WriteData(bytes, 0, bytes.Length);
    waveFileWriter.Flush();
}

当我运行上面的代码时,我收到的所有字节数组都是0。但是如果使用WaveFileWriter将数据直接写入文件,该文件将接收正确的数据。有什么理由吗?

2 个答案:

答案 0 :(得分:5)

尝试一下:

using (WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(new Mp3FileReader(inputStream))) 
using (WaveFileWriter waveFileWriter = new WaveFileWriter(outputStream, waveStream.WaveFormat)) 
{ 
    byte[] bytes = new byte[waveStream.Length]; 
    waveStream.Position = 0;
    waveStream.Read(bytes, 0, waveStream.Length); 
    waveFileWriter.WriteData(bytes, 0, bytes.Length); 
    waveFileWriter.Flush(); 
} 

答案 1 :(得分:1)

如果您要写入MemoryStream,则需要注意,WaveFileWriter将在处置MemoryStream之后被处置WaveFileWriter

这是使用IgnoreDisposeStream的一种解决方法。 (还请注意,WaveFormatConversionStream.CreatePcmStream是不必要的-Mp3FileReader已经从Read返回PCM)。我也更喜欢阅读较小的块,以尝试通过整个文件。

var path = @"mytestFile.mp3";
var mp3ByteArray = File.ReadAllBytes(path);

var outputStream = new MemoryStream();
using (var mp3Stream = new MemoryStream(mp3ByteArray))
using (var reader = new Mp3FileReader(mp3Stream))
using (var waveFileWriter = new WaveFileWriter(new IgnoreDisposeStream(outputStream), 
                                               reader.WaveFormat))
{
    byte[] buffer = new byte[reader.WaveFormat.AverageBytesPerSecond];
    int read;
    while((read = reader.Read(buffer,0, buffer.Length)) > 0)
    {
        waveFileWriter.Write(buffer, 0, read);
    }
}

// outputStream has not yet been disposed so we can get the byte array from it
var wavBytes = outputStream.GetBuffer();

// or we could play it like this
outputStream.Position = 0;
using (var player = new WaveOutEvent())
using (var reader = new WaveFileReader(outputStream))
{
    player.Init(reader);
    player.Play();
    while(player.PlaybackState != PlaybackState.Stopped)
    {
        Thread.Sleep(1000);
    }
}