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

时间:2016-05-30 07:24:10

标签: c# asp.net-mvc naudio naudio-framework

这里我有一些编码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);
                }

            }
        }

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

1 个答案:

答案 0 :(得分: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);




            }


        }