My application does this, play the selected sound using wmplib and upon statechangeevent = 1 (media player stopped), i will close the player using the close() command and setting the URL to "";
Now every time, i tried to delete a sound that is currently playing, in actual fact, it will be deleted, because when I looked at the directory, the file has been deleted. Every time after I delete a file, I will reload the controls which will list be the media file within the directory. However, it will still find the sound that is already been deleted in the directory.
I have tried this way, debugging slowly, and I found out that the File info delete() command takes a longer time to delete, being the fact that closing the media player takes a longer time for it to delete, thats why whenever I try reloading the collections after deleting a currently playing song, the deleted song is still there. When I tried to delete when the media player is not playing the song, it deletes fast and the reloading collections did show the result of the song being gone.
The question is, how can I delete the file that is currently being played, ensuring that the reloading collections will show the correct number of song in the directory?
Any help would be greatly appreciated.
My code is as below.
DirectoryInfo di = new DirectoryInfo(@"C:/Sounds/Custom Sounds/");
FileInfo[] filepaths = MultipleFileFilter(di, "*.mp3,*.wma,*.wav");
for (int i = 0; i < filepaths.Length; i++)
{
if (Path.GetFileNameWithoutExtension(Convert.ToString(filepaths[i]))== deleteItem)
{
System.IO.FileInfo fi = new System.IO.FileInfo(@"C:/Sounds/Custom Sounds/" + Convert.ToString(filepaths[i]));
fi.Delete();
}
}
// Load the sounds
DirectoryInfo dii = new DirectoryInfo(@"C:/Sounds/Custom Sounds/");
FileInfo[] filess = MultipleFileFilter(dii, "*.mp3,*.wma,*.wav");
for (int i = 0; i < filess.Length; i++)
{
RadioButton rb = new RadioButton();
rb.Text = Path.GetFileNameWithoutExtension(filess[i].ToString());
rb.Name = Path.GetFileName(filess[i].ToString());
rb.CheckedChanged += new EventHandler(radioButton_Update);
Point lc = new Point(15, yPos);
rb.Location = lc;
panelSound.Controls.Add(rb);
lstbxCustomSounds.Items.Add(Path.GetFileNameWithoutExtension(rb.Text));
yPos += 20;
//add into the arraylist
controlsCollections.Add(rb);
}
Maybe this is the problem? Because only the currently playing sound that is being deleted will cause this problem?
private void wPlayer_PlayStateChange(int newstate)
{
if (newstate == 1 )//Media finished
{
btnPlay.Text = "Play";
wplayer.close();
btnDelete.Enabled = true;
wplayer.URL = "";
}
else if (newstate == 3)
{
btnDelete.Enabled = false;
}