我正在通过Web服务成功捕获WAV文件,并能够在Windows 7计算机上播放该文件。现在,我需要获取相同的WAV文件并将其转换为MP3文件。这是失败的。我尝试将文件作为WAV检索(上图),然后使用-
WaveStream PCMStream = WaveFormatConversionStream.CreatePcmStream(reader);
将此muLaw转换为PCM。
此操作失败,并附带错误。我曾尝试直接使用LameMP3FileWriter进行转换,但这也无法告诉我“ muLaw格式无法转换为MP3。
我想念的是什么?代码片段
private void WriteMP3(string iNum, string CallId)
{
//construct the replay URL using the iNum
string url = "http://" + _VerintServer + "/searchapi?command=replay&id=" + iNum;
// name the new mp3 file
string mp3File =_basePath + "mp3\\" + CallId + ".mp3";
string tempWav = _basePath + "mp3\\" + CallId + ".wav";
string tempPCM = _basePath + "mp3\\" + CallId + "_PCM.wav";
//issue the web service request
HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create(url);
//retrieve the content type as raw PCM (.wav)
request.ContentType = "audio/wav";
//encode and include the basic authentication credentials
string encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(_username + ":" + _password));
request.Headers.Add("Authorization", "Basic " + encoded);
//retrieve the response
HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
//copy the stream of bytes to the local directory using the CallId as the file name
if (request.HaveResponse)
{
//create a temporary wav file
using (var receiveStream = response.GetResponseStream())
{
using (var PStream = new FileStream(tempWav, FileMode.Create,FileAccess.Write))
{
receiveStream.CopyTo(PStream);
}
}
WaveToPCM(tempWav, tempPCM);
WaveToMP3(tempPCM, mp3File, 128);
}
return;
}
//Convert muLaw WAV to PCM
private void WaveToPCM(string waveFileName, string pcmFileName)
{
using (var reader = new CueWaveFileReader(waveFileName))
{
WaveStream PCMStream = WaveFormatConversionStream.CreatePcmStream(reader);
using (var PStream = new FileStream(pcmFileName, FileMode.Create, FileAccess.Write))
{
PCMStream.CopyTo(PStream);
}
}
return;
}
// Convert WAV to MP3 using libmp3lame library
public void WaveToMP3(string waveFileName, string mp3FileName, int bitRate = 128)
{
using (var reader = new WaveFileReader(waveFileName))
using (var writer = new LameMP3FileWriter(mp3FileName, reader.WaveFormat, bitRate))
reader.CopyTo(writer);
}
堆栈跟踪:
{“ $ id”:“ 1”,“ Message”:“发生错误。”,“ ExceptionMessage”:“非WAVE文件-没有RIFF标头”,“ ExceptionType”:“ System.FormatException”,“ StackTrace“:”在NAudio.FileFormats.Wav.WaveFileChunkReader.ReadRiffHeader(BinaryReader br)\ r \ n在NAudio.FileFormats.Wav.WaveFileChunkReader.ReadWaveHeader(Stream stream)\ r \ n在NAudio.Wave.WaveFileReader..ctor(在C:\ Codebank \ DataAccess \中的NAudio.Wave.WaveFileReader..ctor(String waveFile)\ r \ n在DataServices.Controllers.RecordingsController.WaveToMP3(String waveFileName,String mp3FileName,Int32 bitRate)处输入流InputStream)\ r \ n DataServices \ Controllers \ RecordingsController.cs:行179 \ r \ n,位于DataServices.Controllers.RecordingsController.WriteMP3(String iNum,
在WaveToPcm
函数中,您只是将原始样本写入文件中。您需要使用WaveFileWriter.CreateWaveFile
传入PCMStream
来制作可以传递给LAME MP3 Writer的16位PCM WAV文件]
谢谢马克。我现在已成功将muLaw编码的WAV文件转换为原始PCM,如下面的代码所示。
//Convert muLaw WAV to PCM
private void WaveToPCM(string waveFileName, string pcmFileName)
{
using (var reader = new WaveFileReader(waveFileName))
{
WaveStream PCMStream = WaveFormatConversionStream.CreatePcmStream(reader);
WaveFileWriter.CreateWaveFile(pcmFileName, PCMStream);
}
return;
}