简体   繁体   中英

Naudio: How to play MP3 and WAV file?

Okay, I know this sounds like a very easy question to some but I am really stuck here. Indeed, I am building an audio player using Naudio and I have realized that in many tutorials people always show easy ways to get you started. However, in my opinion, they always forget to show how things are actually done in a real application. For example, when playing music with Naudio, I would do something like:

  Void PlayAudioMusic(string FilePath)

  {

     using (var ms = File.OpenRead(FilePath))
    using (var rdr = new Mp3FileReader(ms))
    using (var wavStream = WaveFormatConversionStream.CreatePcmStream(rdr))
    using (var baStream = new BlockAlignReductionStream(wavStream))
    using (var waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
    {
        waveOut.Init(baStream);
        waveOut.Play();

    }
 }

This is great for testing in a simple console application. This however isn't useful if you're actually building a serious application. For example, what many tutorials never say is for example how to handle the most critical things such as:

  1. Disposing resource and when to do it
  2. The best ways to handle different exceptions
  3. What to do before you pause, stop, rewind or even exit the application
  4. Other stuffs I don't even know exist. Since I am going through this process and have notice that my application has way too many exceptions thrown, can someone please share like a wrapper class around Naudio that he used to handle this. I am sure this will answer many of the trouble some of us have been going through when trying to use Naudio.

Thanks.

  1. To Dispose the unmanaged resources, you call the Close method of the WaveStreams. The "when to do it" part is rather obvious... Do you really don't know when it is the right time to Dispose unmanaged resources? You Dispose them when you are not going to use them anymore.
  2. I can't answer this one. Sorry.
  3. To Pause, you call the Pause method of the WaveOut object. To rewind, you call the Seek method of the WaveStream. To Stop, DON'T call the Stop method of the WaveOut. You must call Pause and then call the Seek method of the WaveStream to go to the beginning of the buffer.
  4. The most probable cause of all the Exceptions being thrown is because most of the code you shown is actually unnecessary. All you should need to do to play a MP3 file is:

.

WaveStream mainOutputStream = new MP3FileReader(path_of_the_file_you_want_to_play);
WaveChannel32 volumeStream = new WaveChannel32(mainOutputStream);

WaveOutEvent player = new WaveOutEvent();

player.Init(volumeStream);

player.Play();

I personally prefer to use WaveOutEvent instead of WaveOut because it does not require you to be using Forms nor WPF, enabling you to use NAudio for absolutely any kind of application you want make with C#, even XNA games. Also, WaveOutEvent has a very fire-and-forget usability, and it's constructor not even asks for a Callback.

All these WaveStreams meant for changing stuff about the buffer (such as Sample Rate of Bit Depth) are just ways of asking for NAudio to throw an exception. They rarely work when used like this. If you want to convert some stuff of the buffers, you have to call some Static methods of the WaveFormatConversionStream (their names are self-explanatory, at least.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM