Click here to Skip to main content
15,860,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey I am developing a program that triggers music layers based on your movements in front of the Kinect.

But It should not trigger a sound that is already playing. How do I check if the sound-file I want to play is already playing?

I am using C#


Thx in advance :)
Posted
Comments
Ron Beyer 19-Jan-14 20:22pm    
Can you post the code on how you play the file? We need to know what API's you are using. What platform is this for, Windows or XBox?
Member 10405651 19-Jan-14 20:33pm    
My program is a windows application and I use this code:

System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"L:\NOT LIBRARIES\snare drum\Ghetto Elec Snares\GE_SNR03.wav");
player.Play();

But I believe that other ways to play the audio may have more functions.

Thx :)
Member 10405651 19-Jan-14 20:42pm    
I use this code. But I kinda believe that Media.soundplayer does not have this function. My program is a windows aplication.

System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"L:\NOT LIBRARIES\snare drum\Ghetto Elec Snares\GE_SNR03.wav");
player.Play();

Thx :)
BillWoodruff 19-Jan-14 20:42pm    
Can you use the 'LoadCompleted Event of the SoundPlayer class to keep track of what's being played ? Or, perhaps it is the case you want to check to see if some other process is currently playing sound ?
Member 10405651 19-Jan-14 20:42pm    
I use this code. But I kinda believe that Media.soundplayer does not have this function. My program is a windows aplication.

System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"L:\NOT LIBRARIES\snare drum\Ghetto Elec Snares\GE_SNR03.wav");
player.Play();

Thx :)

1 solution

The only way to do this with the SoundPlayer class is to use the PlaySync[^] method using a new thread. When the thread completes, the sound is over.

So, in C# pseudo-code, you would have something like this:

C#
public class SoundEffect
{
    string _soundFile;
    Thread _soundThread;
    bool _isStopped = true;

    public bool IsFinished { get { return _isStopped; } }    

    public SoundEffect(string soundFile)
    {
        _soundFile = soundFile;
    }

    public void Play()
    {
       if (!_isStopped)
           return;

       _soundThread = new Thread(PlayThread);
       _soundThread.Start();
    }

    private void PlayThread()
    {
        _isStopped = false;
        System.Media.SoundPlayer player = new System.Media.SoundPlayer(_soundFile);
        player.PlaySync();
        _isStopped = true;
    }
}


Then you can see if the sound is complete or not.
 
Share this answer
 
v3
Comments
Member 10405651 19-Jan-14 20:58pm    
Thank you :)

I will look into how PlaySync[^] works, but I will keep this thread open a little time if anyone would have a solution that is not pseudo code, so I can test it right away.

btw I cant see how much of what you posted is pseudo code and how much is real.
Ron Beyer 19-Jan-14 20:59pm    
Almost all of it is "real", you will need a little more code to create the thread, but other than that it should almost work out of the box.
Ron Beyer 19-Jan-14 21:01pm    
BTW, I edited it a little to add a property for knowing if its finished or not.
Member 11763111 3-Jul-15 6:05am    
Can you provide me the full code so that o can test it and understand it better?
Member 10405651 19-Jan-14 21:06pm    
holy smokes I just looked up what a thread is, and I believe it is above what I understand. But your help definitely made me come closer to my goal Thanks :)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900