wma压缩音频文件使用Naudio抛出错误

Posted

技术标签:

【中文标题】wma压缩音频文件使用Naudio抛出错误【英文标题】:wma compression audio files throwing error using Naudio 【发布时间】:2016-05-30 07:24:09 【问题描述】:

这里我有一些用于编码 wma 音频文件的代码。它工作得很好。但是将输出文件上传到服务器,发生了一些错误。它显示的输出文件应该符合 率等要求应该在16000以内

public void ConvertToWMA(string tempFilePath, string tempFileName, string audioType)
        
          WaveFormat form = new WaveFormat(16000, 16, 1);
          using (WmaStream str = new WmaStream(tempFilePath + tempFileName, form))
            
                string profileData;
                using (StreamReader reader = new StreamReader(File.OpenRead("audio.prx")))
                
                    profileData = reader.ReadToEnd();
                

                IWMProfileManager profileManager;
                IWMProfile wmProfile = null;
                profileManager = WM.CreateProfileManager();
                profileManager.LoadProfileByData(profileData, out wmProfile);

                WMProfile wmp = new WMProfile(str.Profile);

                NAudio.WindowsMediaFormat.WmaWriter ww = new NAudio.WindowsMediaFormat.WmaWriter(new FileStream(@"D:\wma\conv\test.wma", FileMode.Create), form, wmProfile);
                byte[] buff = null;

                int read = 0;

                buff = new byte[form.AverageBytesPerSecond];
                read = str.Read(buff, 0, buff.Length);
                while ((read > 0))
                
                    ww.Write(buff, 0, read);
                    read = str.Read(buff, 0, buff.Length);
                

            
        

如何才能摆脱这个问题。有人请帮帮我..

【问题讨论】:

这里的赏金是怎么回事?当赏金被添加并且赏金由具有接受答案的人添加时,看起来问题已经有一个可接受的答案。 @Yaur 我只需要明确的答案,比我更好。 【参考方案1】:
  
            var temp = tempFilePath + tempFileName;
            using (var reader = new MediaFoundationReader(temp))
            
                // Create a wave format for 16-bit pcm at 8000 samples per second.
                int channels = reader.WaveFormat.Channels;
                int rate = 8000;
                int rawsize = 2;
                int blockalign = rawsize * channels; // this is the size of one sample.
                int bytespersecond = rate * blockalign;
                //MediaFoundationEncoder.enc(reader, "test.mp3");
                var midformat =
                    WaveFormat.CreateCustomFormat(WaveFormatEncoding.Pcm,
                                                   rate,
                                                   channels,
                                                   bytespersecond,
                                                   blockalign,
                                                   rawsize * 8);

                // And a conversion stream to turn input into 16-bit PCM.
                //var midstream = new MediaFoundationResampler(reader, midformat);
              //  var outstream = new PcmToALawConversionStream(midstream);
             //   var outstream = new PcmToALawConversionStream(midstream);
                //var converted16Bit = new SampleToWaveProvider16(mixer);
//
                // now for MP3, we need to upsample to 44.1kHz. Use MediaFoundationResampler 
                using (var resampled = new MediaFoundationResampler(
                            reader, midformat))
                
                    var outstream = new PcmToALawConversionStream(resampled);
                  //  var desiredBitRate = 16000; // ask for lowest available bitrate 

                    //MediaFoundationEncoder.EncodeToWma(outstream,
                    //                "mixedtets10.wma", desiredBitRate);
                    WaveFileWriter.CreateWaveFile("mixedtets10.wma", outstream);
                    //NAudio.WindowsMediaFormat.WmaWriter ww = new NAudio.WindowsMediaFormat.WmaWriter(new FileStream(@"D:\wma\conv\test1.wma", FileMode.Create), midformat, outstream);
                


              //  NAudio.WindowsMediaFormat.WmaWriter ww = new NAudio.WindowsMediaFormat.WmaWriter(new FileStream(@"D:\wma\conv\test1.wma", FileMode.Create), midformat, outstream);

                //NAudio.WindowsMediaFormat.WmaWriter Ww=
                // The output stream is our custom stream.
                //var outstream = new PcmToALawConversionStream(midstream);




            


        

【讨论】:

以上是关于wma压缩音频文件使用Naudio抛出错误的主要内容,如果未能解决你的问题,请参考以下文章

使用 NAudio 从内存流中播放 .wma

使用 C# 中的 NAudio 将 M4A 音频文件转换为 MP3 的问题/错误

使用 NAudio 混合两个音频文件

使用 NAudio 替换视频的音频流

使用 NAudio 1.7+ 播放音频文件

c#的音频库[关闭]