To stop a melody before it completes, you can pass in an empty melody. 

A melody is an array of notes, so you can do this by going into the javascript view and passing in an empty array, then going back to blocks mode.

input.onButtonPressed(Button.A, () => {
    music.beginMelody(music.builtInMelody(Melodies.Birthday), MelodyOptions.Once)
input.onButtonPressed(Button.B, () => {
    music.beginMelody([], MelodyOptions.Once)