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);
}
}