pxt-calliope/docs/reference/music/on-event.md
Amerlander 918af4f3ac
Bump V3.0.22 ()
* change simulator svg

* change radio image

* Remove google fonts cdn

* change color of 'advanced' button

* font fix

* font fix 2

* display fix

* change fullsceen simulator bg

* Continuous servo

* handle continuous state

* adding shims

* update rendering for continuous servos

* fixing sim

* fix sig

* typo

* fix sim

* bump pxt

* bump pxt

* rerun travis

* Input blocks revision

- add Button and Pin event types
- merge onPinPressed & onPinReleased in new onPinEvent function
- create new onButtonEvent function

* update input blocks in docs and tests

* remove device_pin_release block

* Hide DAL.x behind Enum

* bring back deprecated blocks, but hide them

* shims and locales files

* fix input.input. typing

* remove buildpr

* bump V3

* update simulator aspect ratio

* add Loudness Block

* revoke loudness block

* Adds soundLevel

To be replaced by pxt-common-packages when DAL is updated.

* Remove P0 & P3 from AnalogPin

Co-authored-by: Juri <gitkraken@juriwolf.de>
2020-09-08 02:04:25 -07:00

66 lines
1.8 KiB
Markdown

# On Event
Raises events for melodies or music events.
```sig
music.onEvent(MusicEvent.MelodyNotePlayed, () => {})
```
## Parameters
* ``value`` the kind of event
* ``handler`` the code to run when the event is raised.
## Example
This example prints all the events to the serial output.
```blocks
music.onEvent(MusicEvent.MelodyRepeated, () => {
serial.writeLine("melody repeated")
})
music.onEvent(MusicEvent.MelodyEnded, () => {
serial.writeLine("melody ended")
})
music.onEvent(MusicEvent.MelodyStarted, () => {
serial.writeLine("melody started")
})
music.onEvent(MusicEvent.MelodyRepeated, () => {
serial.writeLine("background melody repeated")
})
music.onEvent(MusicEvent.BackgroundMelodyStarted, () => {
serial.writeLine("background started")
})
music.onEvent(MusicEvent.BackgroundMelodyEnded, () => {
serial.writeLine("background ended")
})
music.onEvent(MusicEvent.BackgroundMelodyPaused, () => {
serial.writeLine("background paused")
})
music.onEvent(MusicEvent.BackgroundMelodyResumed, () => {
serial.writeLine("background resumed")
})
music.onEvent(MusicEvent.BackgroundMelodyRepeated, () => {
serial.writeLine("background repeated")
})
input.onButtonPressed(Button.A, () => {
music.beginMelody(music.builtInMelody(Melodies.BaDing), MelodyOptions.Once)
})
music.setTempo(100)
music.beginMelody(music.builtInMelody(Melodies.Ringtone), MelodyOptions.ForeverInBackground)
```
## Background melody
The events related to background melody get triggered by a melody that is played inside a run in background block.
```
control.inBackground(function () {
basic.pause(randint(0, 5000))
music.beginMelody(music.builtInMelody(Melodies.Entertainer), MelodyOptions.Once)
})
music.onEvent(MusicEvent.BackgroundMelodyStarted, function () {
basic.showIcon(IconNames.EigthNote)
})
```