This project has moved and is read-only. For the latest updates, please go here.

MP3

Convert an MP3 to WAV

1) Add the using references for NAudio to your project:

using NAudio;
using NAudio.Wave;

2) Assuming that you are going to find the location of a MP3 and save the WAV file to the same location:

public static void Mp3ToWav(string mp3File, string outputFile)
{
    using (Mp3FileReader reader = new Mp3FileReader(mp3File))
    {
        WaveFileWriter.CreateWaveFile(outputFile, reader);
    }
}

3) You now have a WAV file in the directory of your MP3.

 

The complete sample program:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using NAudio;
using NAudio.Wave;

namespace ConvertMP3toWAV
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void cmbOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "MP3 Files (*.mp3)|*.mp3|All Files (*.*)|*.*";
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtLocation.Text = openFileDialog.FileName;
            }
        }

        private void cmbConvert_Click(object sender, EventArgs e)
        {
            string outputFileName = txtLocation.Text;
            outputFileName = outputFileName.Substring(0, txtLocation.TextLength - 3) + "wav";
            Mp3ToWav(txtLocation.Text, outputFileName);
        }

        public static void Mp3ToWav(string mp3File, string outputFile)
        {
            using (Mp3FileReader reader = new Mp3FileReader(mp3File))
            {
                WaveFileWriter.CreateWaveFile(outputFile, reader);
            }
        }
    }
}

Last edited Jan 3, 2013 at 11:47 AM by markheath, version 13

Comments

jbrill3000 May 10, 2013 at 10:38 PM 
I forgot to say that the MP3 is from mp3skyperecorder I am using 24 bit, at 16 kHz

jbrill3000 May 10, 2013 at 10:35 PM 
I am using this code to convert from MP3 to WAV but the original MP3 file is 55minutes and 4 secs and the wav file is 54 minutes and 5 seconds. Is there a way to make the files come out to be the same length by time? It is critical that they be the same length for my application. Any ideas?

slygamer Dec 13, 2012 at 1:34 PM 
@D1mex: This is a partial class of a WinForms form. It is assumed that you have a form that contains a Label called txtLocation, a Button called cmbOpen and Button called cmbConvert. The Click events of those buttons are linked to cmbOpen_Click and cmbConvert_Click respectively.

D1mex Nov 24, 2012 at 6:37 AM 
"txtLocation" not in the present context?

solsystem Sep 3, 2012 at 9:32 PM 
Can we get progressbar value of conversion progress?

markheath Jan 27, 2012 at 7:08 PM 
NAudio does not convert to MP3. I suggest using LAME.exe to do this

distorsion13 Oct 20, 2011 at 7:43 PM 
Do you have the inverse way--> Wav to Mp3 ???