I would like help, I have the following situation.
I have a web application in which to return an audio file according to the requests. However (I believe) because these audios are of .wav format I could not listen to them, however when returning a recording .mp3 it was possible to listen to it normally.
I tried to implement some way to convert the .wav files to .mp3 using the NuGet package NAudio with the code below but I did not succeed.
private void ConvertWavMP3(string wavFile)
{
try
{
using (var wavRdr = new WaveFileReader(wavFile))
using (var mp3Writer = new LameMP3FileWriter(wavFile.Replace(".wav", ".mp3"), wavRdr.WaveFormat, 128))
{
wavRdr.CopyTo(mp3Writer);
}
}
catch (Exception ex)
{
MessageBox.Show("Error converting wav to mp3.", "CONVERSION ERROR");
}
}
private static void ConvertMp3ToWav(string mp3File)
{
try
{
using (Mp3FileReader reader = new Mp3FileReader(mp3File))
{
//using (WaveStream pcmStream = new WaveFormatConversionStream(new WaveFormat(8000, 16, 1), reader))
using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(reader))
{
WaveFileWriter.CreateWaveFile(mp3File.Replace(".mp3", ".wav"), pcmStream);
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error converting mp3 to wav.", "CONVERSION ERROR");
}
}