Audio file conversion . Wav to . Mp3

Asked

Viewed 238 times

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 format . wav I could not hear them, but 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 Nuget package Naudio with the code below but was not successful.

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");
    }
}
  • Enter the code you used for your initial attempt, which we will help.

  • Sorry I ended up saving the question without the code! Changed!

  • Bit rate is wrong, use LamePreset.ABR_128 in place of 128, in using (var mp3Writer = new LameMP3FileWriter(wavFile.Replace(".wav", ".mp3"), wavRdr.WaveFormat, 128)) { ... }

  • I believe this approach can work: https://stackoverflow.com/a/22889091/4312593

  • @Marcelouchimura as I return the converted file ?

  • @Victoraugusto, you are using which version of ASP.NET?

  • @Marcelouchimura 4.5.2

Show 2 more comments

1 answer

2

Bit rate is wrong, use LamePreset.ABR_128 in place 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 URL bar of the browser, type http://www.meusite.com.br/meuhandlerdemp3.ashx?f=Metallica%20%2d%20Hero%20Of%20The%20Day.mp3

EDIT: On iOS/Safari, try creating an aspx page with a tag <audio>:

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

EDIT 2: your file . wav 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");
    }
}
  • Perfect worked perfectly ,only a doubt to other files, not audios as it works in the same way?

  • Yes. Just change the content type, depending on the file case.

  • Guy I made a test here in the browser on IOS and the file did not open, have any idea what might be ? on Android worked normal

  • I think it has to be inside the tag <audio>

  • Here https://discussions.apple.com/thread/3040776 has a thread saying that the MP3 content type has to be audio/x-mpeg, on Safari.

  • Repositorio’s original audio file is . wav would not need to convert it ?

  • Need it; convert it with the code you made, using my caveats, saving it in a folder. And then in Handler ashx, take that same folder.

  • I have the following error when writing the new file "Unsupported encoding format Alaw Parameter name: format"

  • I edited the answer.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.