如何将 NAudio WaveStream 写入内存流?

How to write NAudio WaveStream to a Memory Stream?(如何将 NAudio WaveStream 写入内存流?)
本文介绍了如何将 NAudio WaveStream 写入内存流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I have a program that takes in mp3 data in a byte array. It has to convert that mp3 data into wav format and store it in a byte data. I am trying to use NAudio for this purpose. I am using the following code for this purpose.

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 将数据直接写入文件,则文件会收到正确的数据.有什么原因吗?

When I run the above code, all I receive is 0 in the byte array. But if use WaveFileWriter to write the data directly to a file, the file receives the correct data. Any reasons?

推荐答案

试试这个:

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

这篇关于如何将 NAudio WaveStream 写入内存流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to know if a field is numeric in Linq To SQL(如何在 Linq To SQL 中知道字段是否为数字)
Extract sql query from LINQ expressions(从 LINQ 表达式中提取 sql 查询)
LINQ Where in collection clause(LINQ Where in collection 子句)
Orderby() not ordering numbers correctly c#(Orderby() 没有正确排序数字 c#)
Why do I get quot;error: ... must be a reference typequot; in my C# generic method?(为什么我会收到“错误:...必须是引用类型?在我的 C# 泛型方法中?)
Strange LINQ Exception (Index out of bounds)(奇怪的 LINQ 异常(索引越界))