Results 1 to 2 of 2

Thread: Help with SoundPlayer Event

  1. #1

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Help with SoundPlayer Event

    hi guys! how can I have the Event or what is the event raised when my SoundPlayer is done playing the sound? Thanks in advance!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help with SoundPlayer Event

    There isn't one. If that's important to you then rather than calling Play and implicitly creating a worker thread to play the sound, you could explicitly create a worker thread and call PlaySync instead. When PlaySync returns the sound has finished playing. If you use a BackgroundWorker you can call its RunWorkerAsync method, then create a SoundPlayer and call its PlaySync method in the DoWork event handler. The BackgroundWorker's RunWorkerCompleted event will be raised when the sound has finished playing.
    Code:
    private void Form1_Load(object sender, EventArgs e)
    {
        this.backgroundWorker1.RunWorkerAsync("WAV file path here");
    }
    
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        using (System.Media.SoundPlayer sp = new System.Media.SoundPlayer((string)(e.Argument)))
        {
            sp.PlaySync();
        }
    }
    
    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("The sound has finished playing");
    }
    Last edited by jmcilhinney; Dec 6th, 2006 at 09:39 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width