1

I have Play and Stop buttons and I want to combine them into one button which works in two functions. However, I couldn't find any WhilePlaying() or AfterPlay() events. I googled some and I've found that using Thread may control events that I want.

Here is what I want. When User clicks the Play Button, Play icon will change to Stop icon and user will be able stop playing immediately or if sound finishes playing Stop icon will change to Play icon.

Here are the codes that I used in my program:

        private void btPlayFile_Click(object sender, EventArgs e)
        {
        try
        {                
            wavPlayer = new SoundPlayer();
            string promptPath = System.Configuration.ConfigurationSettings.AppSettings["PromptStorage"];
            wavPlayer.SoundLocation = promptPath + tePromptFile.Text;
            wavPlayer.LoadCompleted += new AsyncCompletedEventHandler(wavPlayer_LoadCompleted);
            wavPlayer.LoadAsync();

        }
        catch (Exception ex)
        {

            XtraMessageBox.Show("PlayFile Hata:" + ex.Message);
        }
    }

    void wavPlayer_LoadCompleted(object sender, AsyncCompletedEventArgs e)
    {
        try
        {
            ((System.Media.SoundPlayer)sender).Play();

        }
        catch (Exception ex)
        {

            XtraMessageBox.Show("PlayFile Hata:" + ex.Message);
        }
    }

    private void btStopFile_Click(object sender, EventArgs e)
    {
        try
        {
            wavPlayer.Stop();
        }
        catch (Exception ex)
        {
            XtraMessageBox.Show("PlayFile Hata:" + ex.Message);
        }
    }               
cihadakt
  • 3,054
  • 11
  • 37
  • 59
  • cant you just use a simple bool? when pressed play the bool is true for playing and when you press the button it does the waveplayer.stop() – Henrik Bøgelund Lavstsen Jan 31 '13 at 07:22
  • ok but when the player finishes playing it will automatically change the Stop icon to Play icon on the button. How can we control this using a boolean? – cihadakt Jan 31 '13 at 07:29
  • Yeah you are right, this is quite annoying, if you know how to find the length of the wavefile you could make a new class inherit the soundplayer adding a waveEnd event, even tho that kinda seems like quite some work for start stop button – Henrik Bøgelund Lavstsen Jan 31 '13 at 07:54

0 Answers0