When playing sound effects, the response time from requesting playback until actually playing becomes important. In this situation, the SoundEffect element comes in handy. By setting up the source property, a simple call to the play function immediately starts playback.
This can be utilized for audio feedback when tapping the screen, as shown below.
In this example, we want to apply a 180 rotation animation to our Rectangle whenever the "Flip!" button is clicked. We also want to play a different sound when the rectangle flips in one direction or the other.
Notice the ScriptAction { script: swosh.play(); } line. Using the ScriptAction component we can run an arbitrary script as part of the animation, which allows us to play the desired sound effect as part of the animation.
In addition to the play function, a number of properties similar to the ones offered by MediaPlayer are available. Examples are volume and loops. The latter can be set to SoundEffect.Infinite for infinite playback. To stop playback, call the stop function.
When the PulseAudio backend is used, stop will not stop instantaneously, but only prevent further loops. This is due to limitations in the underlying API.