简体   繁体   English

如何将 MP3Sharp 与 IFormFile 一起使用

[英]How to use MP3Sharp with IFormFile

I want to convert an mp3 file to pcm using MP3Sharp ( https://github.com/ZaneDubya/MP3Sharp ) in a web app, where the mp3 file is passed in as IFormFile我想在 web 应用程序中使用 MP3Sharp ( https://github.com/ZaneDubya/MP3Sharp ) 将 mp3 文件转换为 pcm,其中 mp3 文件作为 IFormFile 传入

It works if I save the file to disk first like this...如果我像这样先将文件保存到磁盘,它就会工作......

                using (Stream fileStream = new FileStream("file.mp3", FileMode.Create))
                {
                    await file.CopyToAsync(fileStream);
                }
                MP3Stream stream = new MP3Stream("file.mp3");

... but when I try to do it as a stream without writing to file it doesn't work: ...但是当我尝试将其作为 stream 而不写入文件时它不起作用:

                using (var fileStream = new MemoryStream())
                {
                    await file.CopyToAsync(fileStream);
                    MP3Stream stream = new MP3Stream(fileStream);
                }

The MP3Stream constructor throws this exception: MP3Stream 构造函数抛出这个异常:

MP3Sharp.MP3SharpException: 'Unhandled channel count rep: -1 (allowed values are 1-mono and 2-stereo).' MP3Sharp.MP3SharpException:“未处理的通道计数代表:-1(允许值为 1-mono 和 2-stereo)。”

... Any ideas on what I'm doing wrong? ...关于我做错了什么的任何想法?

After your code does file.CopyToAsync(fileStream) , the MemoryStream 's read/write position is pointing after the written MP3 data at the end of the MemoryStream .在您的代码执行file.CopyToAsync(fileStream)之后, MemoryStream的读/写 position 指向MemoryStream末尾写入的 MP3 数据。

The MP3Stream then trying to read from the MemoryStream will only notice that the end of the MemoryStream has been reached (because its read/write position is already at the end) and throws an exception.然后尝试从MemoryStream读取的MP3Stream只会注意到已经到达MemoryStream的末尾(因为它的读/写 position 已经在末尾)并抛出异常。 [1] [1]

Thus, after copying the MP3 data into the MemoryStream , set the MemoryStream 's read/write position back to where it was before you copied the MP3 data into it (in the case of your example code it's the beginning of the MemoryStream , position 0):因此,将 MP3 数据复制到MemoryStream后,将MemoryStream的读/写 position 设置回将 MP3 数据复制到其中之前的位置(在示例代码的情况下,它是MemoryStream的开头,position 0 ):

await file.CopyToAsync(fileStream);
fileStream.Position = 0;

Side note: Like FileStream and MemoryStream , MP3Stream is also a Stream and therefore an IDisposable , too.旁注:与FileStreamMemoryStream一样, MP3Stream也是一个Stream ,因此也是一个IDisposable And like you already did with the FileStream and the MemoryStream , you should use the using statement for the MP3Stream as well.就像您已经对FileStreamMemoryStream所做的一样,您也应该对MP3Stream使用using语句。


[1] The exception you got from the MP3Sharp library is misleading, and as such is kind of a bug in the library. [1]您从 MP3Sharp 库中得到的异常具有误导性,因此是库中的一个错误。 Because, when attempting to read a byte from a stream and the stream is at its end, the Stream.ReadByte method will return -1 to indicate end-of-stream.因为,当尝试从 stream 读取一个字节并且 stream 在其末尾时, Stream.ReadByte方法将返回 -1 以指示流结束。 And as apparent by the exception message, it seems the MP3Sharp library does not properly treat the -1 value here as simply meaning " the end of the stream has been reached and no (further) data could be read " and misinterprets it as a channel count value.从异常消息中可以明显看出,MP3Sharp 库似乎没有正确地将此处的 -1 值视为简单的意思是“已到达 stream 的末尾并且无法读取(进一步的)数据”并将其误解为通道计数值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM