在这里,我有一个代码将音频文件覆盖为wav格式,以提高质量并减小文件大小。在这里,我使用的是naudio文件压缩源代码,尝试转换该文件时出现异常。
必须已经是浮点数
public string ConvertToWAV(string tempFilePath, string tempFileName, string audioType)
{
//Try to transform the file, if it fails use the original file
FileInfo fileInfo = new FileInfo(tempFilePath + tempFileName);
byte[] fileData = new byte[fileInfo.Length];
fileData = File.ReadAllBytes(tempFilePath + tempFileName);
ISampleProvider sampleProvider;
try
{
if (audioType.ToLower().Contains("wav"))
{
try
{
using (MemoryStream wav = new MemoryStream(fileData))
{
WaveStream stream = new WaveFileReader(wav);
WaveFormat target = new WaveFormat();
var s = new RawSourceWaveStream(new MemoryStream(), new WaveFormat(8000, 16, 1));
var c = new WaveFormatConversionStream(WaveFormat.CreateALawFormat(8000, 1), s);
sampleProvider = new WaveToSampleProvider(c);
WaveFileWriter.CreateWaveFile16(tempFilePath + tempFileName, sampleProvider);
wav.Close();
}
}
catch (Exception ex)
{
//We couldn't convert the file, continue with the original file.
}
}
}
catch (Exception ex)
{
throw ex;
}
return Convert.ToBase64String(fileData);
}
最后,我找到了解决此问题的解决方案,即,需要添加一种名为Media Foundation的附加功能,以便更好地在Windows Server 2012中正常工作。
使用服务器管理器中的“添加角色和功能”向导。跳至功能并选择Media Foundation
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句