首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用C#将任何音频格式转换为MP3

如何使用C#将任何音频格式转换为MP3
EN

Stack Overflow用户
提问于 2012-10-05 23:41:30
回答 1查看 9.7K关注 0票数 0

我想播放用户上传的音频。上传的音频将是任何格式。有没有办法将任何音频文件转换成mp3格式?

EN

回答 1

Stack Overflow用户

发布于 2012-10-05 23:44:27

您可以使用此函数-基于alvas component

链接:http://alvas.net/alvas.audio,tips.aspx#tip24

代码语言:javascript
复制
private void ConvertAudio()
    {
        string webFile = @"D:\AudioCS\bin\Debug\_web.mp3";
        string pcmFile = @"D:\AudioCS\bin\Debug\_AudioCS.wav";
        using (WaveReader wr = new WaveReader(File.OpenRead(pcmFile)))
        {
            IntPtr pcmFormat = wr.ReadFormat();
            byte[] pcmData = wr.ReadData();
            IntPtr webFormat = AudioCompressionManager.GetCompatibleFormat(pcmFormat,
            AudioCompressionManager.MpegLayer3FormatTag);
            byte[] webData = AudioCompressionManager.Convert(pcmFormat, webFormat,
            pcmData, false);
            MemoryStream ms = new MemoryStream();

            using (WaveWriter ww = new WaveWriter(ms,
            AudioCompressionManager.FormatBytes(webFormat)))
            {
                ww.WriteData(webData);
                using (WaveReader wr2 = new WaveReader(ms))
                {
                    using (FileStream fs = File.OpenWrite(webFile))
                    {
                        wr2.MakeMP3(fs);
                    }
                }
            }
        }
    }
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12749641

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档