NAuduio音频格式转换

MP3转wav音频

using System;
using System.Collections.Generic;
using System.Text;
using NAudio;
using NAudio.Wave;

namespace Mp3ToWav
{
    class Program
    {
        static void Main(string[] args)
        {
            string mp3file;
            do 
            {
                do
                {
                    Console.Out.Write("Please enter the mp3 path:");
                    mp3file = Console.In.ReadLine();//读取MP3文件路径
                }
                while (!System.IO.File.Exists(mp3file));
            }
            while (!mp3file.EndsWith(".mp3")) ;
            string wavfile = mp3file.Replace(".mp3", ".wav");//生成输出的wav文件路径
            string wavpath = wavfile;
            //获取音频文件名称以在控制台中显示。
            int index = wavfile.LastIndexOf("\\");
            string wavname = wavfile.Substring(index + 1, wavfile.Length - index - 1);
            index = mp3file.LastIndexOf("\\");
            string mp3name = mp3file.Substring(index + 1, mp3file.Length - index - 1);
            Console.Out.WriteLine("Converting {0} to {1}", mp3name, wavname);
            //第1步:使用Mp3FileReader读取MP3文件。
            using (Mp3FileReader reader = new Mp3FileReader(mp3file))
            {
                //第2步:使用CreatePcmStream方法获取wave流。
                using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(reader))
                {
                    //第3步:用WaveFileWriter将波形数据写入文件。
                    WaveFileWriter.CreateWaveFile(wavfile, pcmStream);
                }
            }
            Console.Out.WriteLine("Conversion finish and wav is saved at {0}.\nPress any key to finish.", wavpath);
            Console.In.ReadLine();
        }
    }
}

你可能感兴趣的:(NAuduio音频格式转换)