Click here to Skip to main content
15,867,939 members
Articles / Programming Languages / C#
Tip/Trick

A Simple C# Player Mp3 with NAudio

Rate me:
Please Sign up or sign in to vote.
4.33/5 (5 votes)
22 Oct 2012CPOL1 min read 85.7K   7.1K   13   6
A Simple C# Player Mp3 with NAudio

Introduction

NAudio is a powerful library distributed with license (MS-PL), for processing and listening of audio files is written in C # and allows you to do much more than you can even think of this kind of files.

Whenever we are faced with such complex projects there is always a great difficulty in looking for starting point of the work, particularly if you do not want to look at all the endless possibilities offered by the library, but we need something simple and stable included in our code. 

Background

NAudio is no exception to this general rule, having to model processes that are by their nature complex. The project site and blog of Mark Heath (the developer) there is a fairly complete documentation and software is available excellent example.

But if we just want to "play" a MP3 file?  

Using the code

I developed a small class c # trying to solve this problem.

The class uses the dll NAudio to play the file and has the following features:

  • full control over the implementation of the audio file (load, play stop and pause)
  • volume control audio level and balance of sound.
  • indication of the execution time 


The routine for me the most important is the one that is invoked to initialize the chain of classes NAudio.

 

C#
public Audio ( string FileName )
{
if (!System.IO.File.Exists(FileName)) return;
// Creazione della classe di interfaccia di riproduzione
   _waveOutDevice = new WaveOut();
   try
   {
     // creazione dello Stram di input dal file dato
    _mainOutputStream = CreateInputStream(FileName);
    if (_mainOutputStream == null)
     {
         throw new InvalidOperationException("Unsupported file extension");
     }
    }
    catch (Exception createException)
    {
      if (Error != null)
        Error("Audio - Play - CreateInputStream", createException.Message, createException.StackTrace);
      return;
    }
    try
    {
    // inizializzazione della risproduzione
    _waveOutDevice.Init(_mainOutputStream)
    }
    catch (Exception initException)
    {
      if (Error != null)
        Error("Audio - Play - Init", initException.Message, initException.StackTrace);
      return;
    }
    // L'evento locale di stop dell'esecuzione viene aggangiato a quello della class NAudio
   
    _waveOutDevice.PlaybackStopped += new 
    EventHandler<stoppedeventargs>(_waveOutDevice_PlaybackStopped);</stoppedeventargs>
    return;
    } 

The class _waveOutDevice is an instance of the class IWavePlayer NAudio that allows the control of reproduction. this class is first created and then initialized.

The routine CreateInputStream 

C#
 private WaveStream CreateInputStream(string fileName)
{
   WaveStream Reader = CreateReaderStream(fileName);
   if (Reader == null)
   {
     throw new InvalidOperationException("Unsupported extension");
   }
   // viene creata una classe WaveChannel32 agganciata allo stram di riproduzione per
   // poter controllare l'esecuzione
   _volumeStream = new WaveChannel32(Reader);
   return _volumeStream;
}

History

The aticle is available in Italian here 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionNeeded dll Pin
Member 113009178-Feb-19 21:25
Member 113009178-Feb-19 21:25 
Questionhow to Normalize audio Pin
soheaila13-Jun-16 3:32
soheaila13-Jun-16 3:32 
QuestionSome problems with this Pin
Tom Jeffries5-Aug-13 8:17
Tom Jeffries5-Aug-13 8:17 
Questiona possible problem about the state indicator? Pin
arash.mhd11-Jun-13 18:32
arash.mhd11-Jun-13 18:32 
AnswerRe: a possible problem about the state indicator? Pin
antrov20-Jun-13 0:39
antrov20-Jun-13 0:39 
QuestionMy Vote of 4 Pin
MarkJoel608-May-13 7:45
MarkJoel608-May-13 7:45 
Thank you for providing this. A simplified demo of NAudio was very helpful to me. The only reason I didn't give it a five was that not all of the events were correctly handled, and it was possible to generate errors when you click exit on the form, depending on the state of the audio file.

That is a small thing though. The audio class you created as an example was very helpful. Thanks!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.