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 8270003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:25:24+00:00 2026-06-08T06:25:24+00:00

I’m using the SoundPlayer class to play WAV files in my WPF application. The

  • 0

I’m using the SoundPlayer class to play WAV files in my WPF application. The files are short and are part of the application and are played in response to events that occur in the program. The user has no control over what sound is played and can’t decide to play a sound on their own. I’ve also tried using the WPF MediaPlayer control, but I had problems with that control. The SoundPlayer is working great, but there’s a problem.

Essentially, I need to keep a queue of sounds and play the sounds one after the other as they queue up. In some circumstances, I have to stop whatever sound is playing and play another sound instead. So I have two requirements which turn out to be mutually exclusive with the SoundPlayer control:

  1. My code needs to know when a sound has finished playing.
  2. The sounds have to play in the background

To implement this, I created a class called SoundController. This has a background thread and it uses the Thread‘s Dispatcher to queue up calls to a method that I use to play the sounds, using BeginInvoke. The method raises an event in the SoundController event before it calls SoundPlayer.PlaySync to play the sound, and then it raises another event after PlaySync returns.

In my UI thread, at the point where I need to stop the sound, I call the SoundController class’s Stop method. This calls the SoundPlayer‘s Stop method to stop playing the sound. And here’s where the problem happens. It turns out that SoundPlayer.Stop does NOT stop the sound, but it waits for the sound to finish before returning.

This stops my GUI dead. While I could call it asynchronously, my GUI won’t stop but the sound won’t stop, either.

As I said, the WPF MediaPlayer didn’t work for us and it was overkill, anyway. Is there any other alternative for playing sounds that will give me the ability to raise events, play the sound in a background thread, and stop the playback?

Edit 06/26/2012:

This has been out here for over a month and I’ve gotten no responses. So I guess there are no other alternatives to the SoundPlayer control.

I’m going to post a new question coming at the problem from a different angle.

  • 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-08T06:25:27+00:00Added an answer on June 8, 2026 at 6:25 am

    There were no responses to this question and I’ve moved beyond this. So, for anyone who’s wondering, the alternatives are the MediaElement WPF control and making WIN32 API calls.

    So which alternative did I use? Neither.

    It turned out that for me, I had to add a second thread to my SoundController class. The second thread does not run a Dispatcher; instead, it runs code I wrote in a loop:

    1. It initially waits indefinitely on an AutoResetEvent.
    2. When the AutoResetEvent is raised, it starts a while loop that checks to see if a sound is to be played.
    3. If a sound is to be be played, it does so.
    4. It loops around and if another sound was “queued” while the last sound is playing, it plays that sound.
    5. This repeats until there are no sounds queued to be played.

    There’s a little bit more but it doesn’t matter. In any event, this seems to work OK. Because the sounds are playing on a separate Thread, the currently playing sound can be aborted from a different Thread by calling the SoundPlayer.Stop method.

    EDIT:

    Here is a stripped down version of my code. I removed some things that were particular to my program’s requirements that had to do with a special sound. This shows how the basic process works.

    private string NextSound { get; set; }
    public AutoResetEvent PlayASoundFlag = new AutoResetEvent( false );
    private Dictionary<string, SoundPlayer> Sounds { get; set; }
    private object soundLocker = new object();
    public string SoundPlaying { get; private set; }
    public bool Stopping { get; set; }
    
    public void PlaySound( string key, bool stopCurrentSound ) {
        if ( !Sounds.ContainsKey( key ) )
            throw new ArgumentException( string.Format( "Sound \"{0}\" does not exist", key ), "key" );
    
        lock ( soundLocker ) {
            NextSound = key;
            if ( SoundPlaying != null && stopCurrentSound )
                Sounds[ SoundPlaying ].Stop();
            PlayASoundFlag.Set();
        }
    }
    
    private void SoundController() {
        do {
            PlayASoundFlag.WaitOne();
            while ( !Stopping && NextSound != null ) {
                lock ( soundLocker ) {
                    SoundPlaying = NextSound;
                    NextSound = null;
                }
                Sounds[ SoundPlaying ].PlaySync();
                lock ( soundLocker )
                    SoundPlaying = null;
            }
        } while ( !Stopping );
    }
    

    When your program starts up, the class loads keys & sound files into the Sounds Dictionary. That way, all the user has to do is pass the key for the sound they want to play to the PlaySound method.

    If you wanted to, you could replace the NextSound property with a Queue. The PlaySound method would have to enqueue the key of the sound to be played and the SoundController thread would have to dequeue each key from the thread in the while loop, exiting when there’s nothing left in the Queue.

    Finally, when your program stops, you need to set the Stopping flag to true & set the PlayASoundFlag so the code comes out of the loop. That will cause the SoundController thread to stop.

    I leave initializing the Dictionary and starting the thread to you.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.