I write the piano. When you press a key, a Click event occurs, a sound is played.

private void Form1_KeyDown(Object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.D1: if (!isPressed) { isPressed = true; button1.PerformClick(); } label5.Text = "1"; break; } private void Form1_KeyUp(Object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.D1: isPressed = false; break; } } public void sound(string path) { var p1 = new MediaPlayer(); p1.Open(new Uri(path, UriKind.Relative)); p1.Play(); } private void button1_Click(Object sender, EventArgs e) { s.sound(@"s//_1.wav"); label3.Text = "До / 1"; } 

Question: how to interrupt the playback of the sound of a note depending on the release of a key? Suppose we press and hold the key, the sound is played completely, when released, the sound stops, while simultaneously pressing the other keys, the sound on all the buttons will be played.

    2 answers 2

    If approached reasonably do so. First, we determine what sounds will be in our piano

     public enum SoundEnum { Do, Re, Mi } 

    Create a collection of sounds (when downloading the application)

     private Dictionary<SoundEnum, MediaPlayer> sounds = new Dictionary<SoundEnum, MediaPlayer>(); 

    The dictionary of the state of clicking on all keys (I can be mistaken with KeyCode, I am not writing in the IDE)

     private Dictionary<KeyCode,bool> pressStates = new Dictionary<KeyCode,bool>(); 

    In the form load method:

     //Инициализируем словари foreach(SoundEnum s in Enum.GetValues(typeof(SoundEnum)).Cast<SoundEnum>()) { sounds.Add(s, new MediaPlayer()); pressStates.Add(s, false); } //Наполняем словарь звуками sounds[SoundEnum.Do].Open(....); sounds[SoundEnum.Re].Open(....); 

    Making the sound play function

     public void PlaySound(SoundEnum sound) { sounds[sound].Play(); } 

    Making the sound interrupt function

     public void StopSound(SoundEnum sound) { sounds[sound].Stop(); } 

    Note Button Matching Dictionary

     private Dictionary<KeyCode,SoundEnum> KeySounds = new Dictionary<KeyCode,SoundEnum>{{Keys.D1,SoundEnum.Do},...}; 

    When you press do

     private void Form1_KeyDown(Object sender, KeyEventArgs e) { if (!KeySounds.ContainsKey(e.KeyCode)) return; if (!pressStates[e.KeyCode]) { pressStates[e.KeyCode] = true; PlaySound(KeySounds[e.KeyCode]) { } 

    When released

     private void Form1_KeyUp(Object sender, KeyEventArgs e) { if (!KeySounds.ContainsKey(e.KeyCode)) return; if (pressStates[e.KeyCode]) { pressStates[e.KeyCode] = false; StopSound(KeySounds[e.KeyCode]) { } 

    If there are still buttons on the form for the mouse - then through mouse events for the buttons by analogy.

    • It is even better to do not the dictionary of the states of the buttons, but the dictionary of the states of the sounds - playing or not - RusArt
    • thanks, now I will try, the studio complains about KeyCode by the way - Valentin
    • @Valentin well, I say, I’m not writing in the studio, substitute the right type - RusArt
    • tried to do, as you described above, gives an error: "This key is not in the dictionary." - Valentin
    • only the keys from D1 to D0 work - Valentin

    For this you can use mouse events for your control. The MouseUp and MouseDown events will do for you. For example, you can set the key sound in the MouseDown event, then it will play only when the mouse button is pressed on the control.

    I provide links to the official documentation in msdn for these events.

    MouseUp MouseDown