c# Naudio MemoryStream 异常不可读

c# Naudio MemoryStream Exception was not readable

我正在使用 Naudio。我正在尝试将 .mp3 流写入 .wav 流。这是我的代码:

        MemoryStream outputStream = new MemoryStream();                
        WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(new Mp3FileReader(@"C:\Users\practice\Desktop\ORIG FILE.mp3"));            
        using (WaveFileWriter waveFileWriter = new WaveFileWriter(outputStream, waveStream.WaveFormat))
        {
            byte[] bytes = new byte[waveStream.Length];
            waveStream.Position = 0;
            waveStream.Read(bytes, 0, (int)waveStream.Length);
            waveFileWriter.Write(bytes, 0, bytes.Length);
            waveFileWriter.Flush();
        }
        using(WaveFileReader reader = new WaveFileReader(outputStream))
        {

        }
        waveStream.Close();
        outputStream.Close();

我在第二个 using 块中遇到异常:Stream was not readable.我该如何解决?

当您的 using 语句完成后,一次性物品将被丢弃。

您可以合并

        using (MemoryStream outputStream = new MemoryStream())
        using (WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(new Mp3FileReader(@"D:\recit.mp3")))
        using (WaveFileWriter waveFileWriter = new WaveFileWriter(outputStream, waveStream.WaveFormat))
        {
            byte[] bytes = new byte[waveStream.Length];
            waveStream.Position = 0;
            waveStream.Read(bytes, 0, (int)waveStream.Length);
            waveFileWriter.Write(bytes, 0, bytes.Length);
            waveFileWriter.Flush();

            outputStream.Position = 0;

            using (WaveFileReader reader = new WaveFileReader(outputStream))
            {}
        }
    }

或将其翻译成 try-catch-finally,您可以在其中手动处理流