.Wav audio file conversion to .Mp3

1

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");
    }
}
    
asked by anonymous 08.06.2018 / 21:46

1 answer

2

The bit rate is wrong, use LamePreset.ABR_128 instead of 128 , in

using (var mp3Writer = new LameMP3FileWriter(wavFile.Replace(".wav", ".mp3"), wavRdr.WaveFormat, 128)) { ... }

To return an MP3 streaming from the file name, do so: add an ashx and implement

public class MeuHandlerDeMp3 : IHttpHandler
{
    private const string CAMINHO_SERVIDOR = @"C:\MP3";

    public void ProcessRequest(HttpContext context)
    {
        string nomeArq = context.Request.QueryString["f"];
        string caminho = Path.Combine(CAMINHO_SERVIDOR, nomeArq);

        if (File.Exists(caminho))
        {
            context.Response.ContentType = "audio/mpeg";
            context.Response.WriteFile(caminho);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

Then, in the browser, call ashx like this: in the browser URL bar, type http://www.meusite.com.br/meuhandlerdemp3.ashx?f=Metallica%20%2d%20Hero%20Of%20The%20Day.mp3

EDIT: In iOS / Safari, try to create an aspx page with a <audio> tag:

<audio controls>
    <source src="meuhandlerdemp3.ashx?f=Metallica - Hero Of The Day.mp3" />
</audio>

EDIT 2: Your .wav file must be very old; do so to convert it to PCM format:

private void ConvertWavMP3(string wavFile)
{
    try
    {
        using (var wavRdr = new WaveFileReader(wavFile))
        using (var pcmReader = WaveFormatConversionStream.CreatePcmStream(wavRdr))
        using (var mp3Writer = new LameMP3FileWriter(wavFile.Replace(".wav", ".mp3"), pcmReader.WaveFormat, 128))
        {
            pcmReader.CopyTo(mp3Writer);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error converting wav to mp3.", "CONVERSION ERROR");
    }
}
    
11.06.2018 / 15:30