gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:41:24 25 4
gpt4 key购买 nike

这里我有一些用于编码 wma 音频文件的代码。它工作得很好。但是将文件上传到服务器时,发生了一些错误。它显示输出文件应该符合要求rate在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);
}

}
}

如何解决这个问题。有人请帮助我..

最佳答案

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




}


}

关于c# - wma压缩音频文件使用Naudio抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37519864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com