问题描述
我正在尝试使用NAudio / NLayer.NAudioSupport创建Azure函数,我可以在其中传递URL(例如https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_1MG.mp3),读取字节并返回以128Kbps转换的mp3文件。但是我目前收到一个异常声明:
System.Private.CoreLib:执行功能时发生异常:ReduceMp3Bitrate。 NAudio.Lame:不支持的编码格式MpegLayer3(参数
format
)。
这是我当前的代码(第5行发生异常):
const string linkUrl = @"https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_1MG.mp3";
var audioFile = new HttpClient().GetByteArrayAsync(linkUrl);
await using var ms = new MemoryStream(await audioFile);
var audioReader = new Mp3FileReader(ms);
await using var audioWriter = new LameMP3FileWriter(@"C:\temp\test.mp3",audioReader.Mp3WaveFormat,LAMEPreset.ABR_128);
await audioReader.copyToAsync(audioWriter);
我认为问题出在audioReader.Mp3WaveFormat
,但是我不确定为什么会成为问题,因为它返回了MpegLayer3
。
我还尝试过在Windows上运行的.Net Framework 4.7控制台应用程序上运行此程序(消除了Azure功能),但仍然无法正常工作。
解决方法
将audioReader.Mp3WaveFormat更改为audioReader.WaveFormat(第5行)。
说明:如https://github.com/naudio/NAudio/blob/master/NAudio/Wave/WaveStreams/Mp3FileReader.cs#L28中所述,Mp3WaveFormat不是Mp3FileReader流的输出格式。在WaveFormat中存在。