For micro:bit v2 only
This block requires the micro:bit V2 hardware. If you use this block with a micro:bit v1 board, you will see the 927 error code on the screen.
Play a song, melody, tone, or a sound effect from a playable music source.
music.play(music.tonePlayable(262, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
Music is played for a simple tone, a melody, or a song. Each of these music sources is called a playable object. The ||music:play||
block can take any of these playable objects and play them as sound output for your game.
The simplest music source is a tone, on note play for a duration of time:
music.play(music.tonePlayable(262, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
Then, there is the melody which is a series of notes played at a certain speed, or tempo
. You can create your own melody of choose a built-in one to play:
music.play(music.stringPlayable("D F E A E A C B ", 120), music.PlaybackMode.UntilDone)
music.play(music.builtInPlayableMelody(Melodies.BaDing), music.PlaybackMode.UntilDone)
The most complex playable object is a sound expression. Sound expressions are composed in the Sound Editor using different parameters for making sound waves and effects..
music.play(music.createSoundExpression(WaveShape.Sine, 5000, 0, 255, 0, 500, SoundExpressionEffect.None, InterpolationCurve.Linear), music.PlaybackMode.UntilDone)
play until done
: play the music source in toPlay but wait to run the next part of the program until music play is done.in background
: play the music source in toPlay but continue with the rest of the program before music play is done.looping in background
: play the music source in toPlay but continue with the rest of the program before music play is done. The music will remain playing, returning to the first note of the music after its duration.
Play a short melody created in the Melody Editor.
music.play(music.stringPlayable("D F E A E A C B ", 120), music.PlaybackMode.UntilDone)
Put 4 different playable music sources in an array. Play one after the other.
let playables = [
music.tonePlayable(262, music.beat(BeatFraction.Whole)),
music.stringPlayable("D F E A E A C B ", 120),
music.builtInPlayableMelody(Melodies.BaDing),
music.createSoundExpression(WaveShape.Sine, 5000, 0, 255, 0, 500, SoundExpressionEffect.None, InterpolationCurve.Linear)
]
for (let someMusic of playables) {
music.play(someMusic, music.PlaybackMode.UntilDone)
basic.pause(500)
}
Play a simple song in the background. When the micro:bit is shaken, stop the song an play the power down
melody.
music.play(music.stringPlayable("C5 A B G A F A C5 ", 120), music.PlaybackMode.LoopingInBackground)
input.onGesture(Gesture.Shake, function () {
music.stopAllSounds()
music.play(music.builtInPlayableMelody(Melodies.PowerDown), music.PlaybackMode.InBackground)
})
Play a sine wave sound effect for 5
seconds.
music.play(music.createSoundExpression(WaveShape.Sine, 5000, 0, 255, 0, 5000, SoundExpressionEffect.None, InterpolationCurve.Linear), music.PlaybackMode.UntilDone)
tone playable, string playable, melody playable, create song, stop all sounds, sound editor, create sound expression