博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# Use NAudio Library to Convert MP3 audio into WAV audio(将Mp3格式转换成Wav格式)
阅读量:6414 次
发布时间:2019-06-23

本文共 4814 字,大约阅读时间需要 16 分钟。

Have you been in need of converting audios to audios?  If so, the skill in this article provides you a chance to manage the mp3-to-wav conversion by yourself.

The wave audio files are useful because they retain the first-generation archived file with high quality and simple file structures. But without a proper tool, it can still be difficult for us to so the conversion. And now I will introduce you to NAudio, an opensource library written in C#, which provide API to meet our needs.

NAudio Overview

is a library with dozens of useful facilities to speed up the development of audio-related project in .NET. The file formats it concerns include .wav, .mp3 and . and the functionalities on these format have undergone a restrict and long-standing testing because the project had been under development since 2001.

Get Started with NAudio

NAudio is distributed as two .dll files, along with an file to facilitate documentation. We can download the latest version of NAudio from and uncompress the .zip file into any directory, which looks like the following

Fig-1: Unzip the archive and release two DLLs and one XML file.

We use to develop our application and Visual C# Express const nothing since it is free online. Then we set up a NAudio environment following the steps below:

  1. create a solution with arbitrary name.
  2. right click the ‘References’ on in the Solution Explorer.
  3. click the ‘Add Reference’, select ‘Browse’ tab.
  4. traverse to directory where we keep our DLLs, select two DLLs and press ‘OK’.

Hence we finishing adding NAudio DLLs into our environment and we can start coding.

Fig-2: Right click the ‘Reference’ and open the dialog.

Fig-3: Traverse to the directory keeping two DLLs.

Fig-4: The NAudio program sets are included in the reference.

Convert MP3 to WAV with NAudio

In NAudio, we are offered many facilities. However, what we need in this example is just one class, NAudio.Wave. So add the following two line beneath the other using commands.

using NAudio;using NAudio.Wave;

To complete conversion from MP3 to Wav, we only need to follow three steps:

  1. read in the mp3 file with Mp3FileReader class.
  2. get wave stream from the MP3 stream via CreatePcmStream interface.
  3. write the wave stream into a file with WaveFileWriter class.

All the three steps above can be easily summarized into the following C# code with comments.

using (Mp3FileReader reader = new Mp3FileReader(mp3file))          {              using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(reader))              {                  WaveFileWriter.CreateWaveFile(wavfile, pcmStream);              }          }

Until now have we finished the work of converting a MP3 file to a Wave file. Surprisingly simple, isn’t it?

Complete Code in C# with NAudio

In this section, I simply produce the complete source code that can be built and run.

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;

//Try to read a mp3 file path until it gets valid one. do { do { Console.Out.Write("Please enter the mp3 path:"); mp3file = Console.In.ReadLine(); } while(!System.IO.File.Exists(mp3file)); } while (!mp3file.EndsWith(".mp3"));

//Generate the wav file path for output. string wavfile = mp3file.Replace(".mp3", ".wav"); string wavpath = wavfile;

 

//Get audio file name for display in console. 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);

 

//Display message. Console.Out.WriteLine("Converting {0} to {1}", mp3name, wavname);

 

//step 1: read in the MP3 file with Mp3FileReader. using (Mp3FileReader reader = new Mp3FileReader(mp3file)) {

//step 2: get wave stream with CreatePcmStream method. using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(reader)) {

//step 3: write wave data into file with 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(); } } }

Conclusion

In this article, I introduce a simplified and self-aided method to convert a mp3 audio file to a wave audio file with .NET library NAudio. To guide you through all the steps, I also show you the steps to include the DLLs into the reference, in which we can access the classes encapsulated in the DLL. And I hope you can find your way to complete this work.

If you like this article, please share it with your friends on facebook, myspace, twitter or any other SNS or media by clicking the retweet buttons. If you have any problem about this article, please contact the author without hesitance.

转:

转载地址:http://qebra.baihongyu.com/

你可能感兴趣的文章
蚂蚁分类信息系统5.8多城市UTF8开源优化版
查看>>
在django1.2+python2.7环境中使用send_mail发送邮件
查看>>
“Metro”,移动设备视觉语言的新新人类
查看>>
PHP源代码下载(本代码供初学者使用)
查看>>
Disruptor-NET和内存栅栏
查看>>
Windows平台ipod touch/iphone等共享笔记本无线上网设置大全
查看>>
播放加密DVD
查看>>
产品设计体会(3013)项目的“敏捷沟通”实践
查看>>
RHEL6.3基本网络配置(1)ifconfig命令
查看>>
网络诊断工具之—路由追踪tracert命令
查看>>
Java模拟HTTP的Get和Post请求(增强)
查看>>
php 环境搭建(windows php+apache)
查看>>
让虚拟机的软盘盘符不显示(适用于所有windows系统包括Windows Server)
查看>>
Cygwin不好用
查看>>
jQuery插件之验证控件jquery.validate.js
查看>>
[经验]无线鼠标和无线键盘真的不能用了?——雷柏的重生之路~
查看>>
【转】plist涉及到沙盒的一个问题
查看>>
GNU make manual 翻译( 一百四十五)
查看>>
重构之美-走在Web标准化设计的路上[复杂表单]3 9 Update
查看>>
linux中的优先搜索树的实现--prio_tree【转】
查看>>