views:

2051

answers:

5

Hey I'm writing a windows forms application which is supposed to play a three sound files and at the end of each sound file it's to change the source of an image.

I can get it to play the sounds using System.Media.SoundPlayer. However it seems to play the sound in a different thread, continuing on.

The net effect of this is that only the last sound is played and all the images are changed.

I've tried Thread.Sleep but it sleeps the whole GUI and after the sleep period everything happens at once and the last sound it played.

Any help would be appreciated.

UPDATE

I thought PlaySynch was working but it seems to freeze my Gui which is less than ideal. Any other suggestions?

+3  A: 

Did you try SoundPlayer.PlaySync Method? From the help:

The PlaySync method uses the current thread to play a .wav file, preventing the thread from handling other messages until the load is complete.

Biri
Yeah I just found this, the only problem is that the Gui doesn't seem to be drawn... Will keep on working on it.
Omar Kooheji
A: 

Instead of using the Play method, use the PlaySync method. Oops, answer has already been posted lol. ;)

RodgerB
A: 

What you probably want to do is do an async sound, but then disable your UI in such a way that it doesn't respond to user response. Then once the sound is played, you re-enable your UI. This would allow you to still paint the UI as normal.

Orion Adrian
A: 

Thanks for the Help Guys I figured out how to do it.

I used PlaySynch but did the playing in a separate thread to the code the draws the UI.

The same thread also updates the UI after each file is played.

Omar Kooheji
A: 

Use this code: [DllImport("WinMM.dll")] public static extern bool PlaySound(byte[]wfname, int fuSound);

//  flag values for SoundFlags argument on PlaySound
public static int SND_SYNC        = 0x0000;      // play synchronously (default)
public static int SND_ASYNC       = 0x0001;      // play asynchronously
public static int SND_NODEFAULT   = 0x0002;      // silence (!default) if sound not found
public static int SND_MEMORY      = 0x0004;      // pszSound points to a memory file
public static int SND_LOOP        = 0x0008;      // loop the sound until next sndPlaySound
public static int SND_NOSTOP      = 0x0010;      // don't stop any currently playing  sound
public static int SND_NOWAIT      = 0x00002000; // don't wait if the driver is busy
public static int SND_ALIAS       = 0x00010000; // name is a Registry alias
public static int SND_ALIAS_ID    = 0x00110000; // alias is a predefined ID
public static int SND_FILENAME    = 0x00020000; // name is file name
public static int SND_RESOURCE    = 0x00040004; // name is resource name or atom
public static int SND_PURGE       = 0x0040;     // purge non-static events for task
public static int SND_APPLICATION = 0x0080;     // look for application-specific association
    private Thread t; // used for pausing
    private string bname;
    private int soundFlags;

//-----------------------------------------------------------------
public void Play(string wfname, int SoundFlags)
{
 byte[] bname = new Byte[256];    //Max path length
 bname = System.Text.Encoding.ASCII.GetBytes(wfname);
            this.bname = bname;
            this.soundFlags = SoundFlags;
            t = new Thread(play);
            t.Start();
}
//-----------------------------------------------------------------

    private void play()
    {
           PlaySound(bname, soundFlags)
    }        

public void StopPlay()
{
           t.Stop();
}

   public void Pause()
   {
           t.Suspend(); // yeah, I know its obsolete, but it works
   }
   public void Resume()
   {
           t.Resume(); // yeah, I know its obsolete, but it works
   }