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, that's 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;
wpl开发者_运维百科ayer.URL = "";
}
else if (newstate == 3)
{
btnDelete.Enabled = false;
}
It seems to be that an easy answer to this problem would be to not allow the deletion of something that is currently being played...either that or ensure that the delete process is complete before reloading the collection.
EDIT Based off your code above, when you're instantiating the fileinfo that is to be deleted, you're appending Convert.ToString(filepath[i])
to the filepath of the new fileinfo object. Instead, I'd get rid of everything before that and just use filepath[i].FullName
.
So I'd just instantiate the object to be deleted as new FileInfo(filepath[i].FullName);
.
精彩评论