Share:

Basic Audio Operations With MP3 And Wave Files Using NAudio C#

In this post, I will discuss about DirectSoundOut, AudioFileReader, Mp3FileReader and WaveFileReader class(es) and its methods. Also, discuss about open, initilize, play, pause, stop and dispose the audio file. Of course! These are basic operations of audio file.

The DirectSoundOut Class

The DirectSoundOut class can initilize, play, pause, stop, dispose the audio files. It is alternate option of WaveOut and WaveOutEvent. See list of methods of DirectSoundOut class.
  • Init()
  • Play()
  • Pause()
  • Stop()
  • Dispose()

The AudioFileReader, Mp3FileReader and WaveFileReader Classes

The AudioFileReader class can automatically decide if file is wave or mp4. When the WaveFileReader and Mp3FileReader can't. If you try to open wave file using Mp3FileReader then it will give you exception "System.FormatException: 'Not a WAVE file - no RIFF header'". And same, If you try to open mp3 file using WaveFileReader then it will give you exception "System.IO.InvalidDataException: 'Invalid MP3 file - no MP3 Frames Detected'".

Work on audio file using NAudio C# simple program

AudioFileReader read = new AudioFileReader("C:/Users/Amit Padhiyar/Desktop/NAudio Tutorial/1.mp3");
DirectSoundOut soundout = new DirectSoundOut();
soundout.Init(read);
soundout.Play();

Thread.Sleep(10000);
soundout.Pause();
Thread.Sleep(3000);


soundout.Play();
Thread.Sleep(10000);

soundout.Stop();
soundout.Dispose();

Work on multiple audio files using NAudio C# program

AudioFileReader AFR1 = new AudioFileReader("C:/Users/Amit Padhiyar/Desktop/NAudio Tutorial/1.mp3");
DirectSoundOut DSO1 = new DirectSoundOut();
DSO1.Init(AFR1);

Mp3FileReader MFR2 = new Mp3FileReader("C:/Users/Amit Padhiyar/Desktop/NAudio Tutorial/1.mp3");
DirectSoundOut DSO2 = new DirectSoundOut();
DSO2.Init(MFR2);

WaveFileReader WFR3 = new WaveFileReader("C:/Users/Amit Padhiyar/Desktop/NAudio Tutorial/1.wav");
DirectSoundOut DSO3 = new DirectSoundOut();
DSO3.Init(WFR3);

DSO1.Play();
DSO2.Play();
DSO3.Play();

Comments

Popular posts from this blog

Get Color From Pixel C# WPF | Saatody | Amit Padhiyar

Create AutoScroll V1_0_0 for WPF C# | Saatody | Amit Padhiyar