Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8358301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:45:07+00:00 2026-06-09T10:45:07+00:00

I’m a little new to using NAudio so I’m surely missing something important, hence

  • 0

I’m a little new to using NAudio so I’m surely missing something important, hence this question.

Starting with example/demo code, I put together a class as simple as I could make it to play MP3 or WAV files:

public class AudioPlayer : IDisposable
{
    WaveStream _waveStream; // sound data stream
    WaveChannel32 _waveChannel32; // sound channel ??
    IWavePlayer _iWavePlayer; // sound output driver

    public bool IsPlaying
    {
        get { return _iWavePlayer != null 
                     && _iWavePlayer.PlaybackState 
                         != Wave.PlaybackState.Stopped; }
    }

    public PlaybackState PlaybackState
    {
        get { return _iWavePlayer.PlaybackState; }
    }

    public void LoadMp3(byte[] mp3Bytes)
    {
        Load(CreateInputStreamFromMp3Bytes(mp3Bytes));
    }

    public void LoadFile(string filename)
    {
        Load(CreateInputStream(filename));
    }

    void Load(WaveStream waveStream)
    {
        if (_iWavePlayer != null)
            Dispose();
        _iWavePlayer = new WaveOut();
        _iWavePlayer.PlaybackStopped += 
            new EventHandler(_iWavePlayer_PlaybackStopped);
        _waveStream = waveStream;
        _waveChannel32 = new WaveChannel32(_waveStream);
        _waveChannel32.PadWithZeroes = false;
        _iWavePlayer.Init(_waveChannel32);
    }

    void _iWavePlayer_PlaybackStopped(object sender, EventArgs e)
    {
        Stop();
    }

    static WaveStream CreateInputStreamFromMp3Bytes(byte[] mp3Bytes)
    {
        return new Mp3FileReader(new MemoryStream(mp3Bytes), false);
    }

    static WaveStream CreateInputStream(string fileName)
    {
        if (fileName.EndsWith(".wav"))
            return new WaveFileReader(fileName);
        else if (fileName.EndsWith(".mp3"))
            return new Mp3FileReader(fileName);
        else
            throw new InvalidOperationException("Unsupported extension");
    }

    public void Play()
    {
        _iWavePlayer.Play();
    }

    public void Stop()
    {
        if (_iWavePlayer != null 
                && _iWavePlayer.PlaybackState != PlaybackState.Stopped) { 
            _iWavePlayer.Stop();
            _waveStream.Position = 0;
        }
    }

    public void Dispose()
    {
        Stop();
        if (_iWavePlayer != null) {
            _iWavePlayer.Dispose();
            _iWavePlayer = null;
        }
        if (_waveChannel32 != null) {
            _waveChannel32.Dispose();
            _waveChannel32 = null;
        }
        if (_waveStream != null) {
            _waveStream.Dispose();
            _waveStream = null;
        }
    }
}

I’m using the code in question to play MP3 files (not WAVs).

It works ok for the most part, which is pretty awesome considering that I’ll be able to replace MediaElements in my project. The issue I’m having is that the PlaybackState stays at Playing rather than changing to Stopped once PlaybackStopped fires. To go around that, I subscribe to PlaybackStopped and call Stop() from there.

Is it normal that I have to call Stop() like that, or is there is something I am missing here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:45:08+00:00Added an answer on June 9, 2026 at 10:45 am

    Looks like a bug. If you look at NAudio code WaveOut will not change PlaybackState in RaisePlaybackStoppedEvent. But DirectSoundOut will change it to stopped. A little crazy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.