我有这段代码:
MediaPlayer player = new System.Windows.Media.MediaPlayer();
bool playing = false;
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D1)
{
if (label5.Text == "Waiting 01.wav")
{
MessageBox.Show("No wav loaded");
return;
}
pictureBox6.Image = Form1.Properties.Resources.white_square_button;
try
{
player.Open(new Uri(label37.Text));
player.Volume = (double)trackBar4.Value / 100;
playing = true;
player.Play();
}
catch (FileNotFoundException)
{
MessageBox.Show("File has been moved." + "\n" + "Please relocate it now!");
}
}
}
private void Window_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D1)
{
pictureBox6.Image = Form1.Properties.Resources.black_square_button;
player.Stop();
player.Close();
playing = false;
}
}
问题是.wav文件只能完成一小部分。
当按下按键D1直到它被释放时它应该完成。
我做错了什么?
答案 0 :(得分:1)
KeyDown事件正在重复。一种解决方案是,如果它是重复键,则立即返回。
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if(e.IsRepeat) return;
// ...