简体   繁体   English

如何使用NAudio将任何音频格式转换为mp3

[英]How to convert any audio format to mp3 using NAudio

public void AudioConvert()
{    
    FileStream fs = new FileStream(InputFileName, FileMode.Open, FileAccess.Read);            
    NAudio.Wave.WaveFormat format = new NAudio.Wave.WaveFormat();
    NAudio.Wave.WaveStream rawStream = new RawSourceWaveStream(fs, format);
    NAudio.Wave.WaveStream wsDATA = WaveFormatConversionStream.CreatePcmStream(rawStream);
    WaveStream wsstream = wst.CanConvertPcmToMp3(2, 44100);
    .....
}

// Here is the class 
public class WaveFormatConversionStreamTests
{
    public WaveStream CanConvertPcmToMp3(int channels,int sampleRate)
    {           
        WaveStream ws = CanCreateConversionStream(
            new WaveFormat(sampleRate, 16, channels),
            new Mp3WaveFormat(sampleRate, channels, 0, 128000/8));
        return ws;
    }
}

Here, i am trying to convert any audio format to mp3 but my code is throwing exception like "ACMNotPossible" at ConvertPCMToMp3 function call. 在这里,我正在尝试将任何音频格式转换为mp3,但是我的代码在ConvertPCMToMp3函数调用时引发了诸如“ ACMNotPossible”之类的异常。 I am using NAudio 1.6 version dll. 我正在使用NAudio 1.6版本dll。 Right now i am working on windows 7. Please tell me where i went wrong in this code. 现在,我正在Windows 7上工作。请告诉我这段代码在哪里出错。

WaveFormatConversionStream is a wrapper around the Windows ACM APIs, so you can only use it to make MP3s if you have an ACM MP3 encoder installed. WaveFormatConversionStream是Windows ACM API的包装,因此,如果安装了ACM MP3编码器,则只能使用它来制作MP3。 Windows does not ship with one of these. Windows不附带其中之一。 The easiest way to make MP3s is simply to use LAME.exe. 制作MP3的最简单方法就是使用LAME.exe。 I explain how to do this in C# in this article . 我解释如何做到这一点在C# 这篇文章

Also, if you are using the alpha of NAudio 1.7 and are on Windows 8 then you might be able to use the MP3 encoder which seems to come with Windows 8 as a Media Foundation Transform. 另外,如果您正在Windows 8上使用NAudio 1.7的alpha版本,则可以将Windows 8附带的MP3编码器用作Media Foundation Transform。 Use the MediaFoundationEncoder (the NAudio WPF demo shows how to do this). 使用MediaFoundationEncoder(NAudio WPF演示显示了如何执行此操作)。

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

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