4

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

            }
        }

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

4

1 回答 1

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);




            }


        }
于 2016-05-31T08:49:04.507 回答