Sound Effects

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.

  1. import QtQuick
  2. import QtMultimedia
  3. Window {
  4. width: 300
  5. height: 200
  6. visible: true
  7. SoundEffect {
  8. id: beep
  9. source: "file:beep.wav"
  10. }
  11. Rectangle {
  12. id: button
  13. anchors.centerIn: parent
  14. width: 200
  15. height: 100
  16. color: "red"
  17. MouseArea {
  18. anchors.fill: parent
  19. onClicked: beep.play()
  20. }
  21. }
  22. }

The element can also be utilized to accompany a transition with audio. To trigger playback from a transition, the ScriptAction element is used.

The following example shows how sound effects elements can be used to accompany transition between visual states using animations:

  1. import QtQuick
  2. import QtQuick.Controls
  3. import QtMultimedia
  4. Window {
  5. width: 500
  6. height: 500
  7. visible: true
  8. SoundEffect { id: beep; source: "file:beep.wav"}
  9. SoundEffect { id: swosh; source: "file:swosh.wav" }
  10. Rectangle {
  11. id: rectangle
  12. anchors.centerIn: parent
  13. width: 300
  14. height: width
  15. color: "red"
  16. state: "DEFAULT"
  17. states: [
  18. State {
  19. name: "DEFAULT"
  20. PropertyChanges { target: rectangle; rotation: 0; }
  21. },
  22. State {
  23. name: "REVERSE"
  24. PropertyChanges { target: rectangle; rotation: 180; }
  25. }
  26. ]
  27. transitions: [
  28. Transition {
  29. to: "DEFAULT"
  30. ParallelAnimation {
  31. ScriptAction { script: swosh.play(); }
  32. PropertyAnimation { properties: "rotation"; duration: 200; }
  33. }
  34. },
  35. Transition {
  36. to: "REVERSE"
  37. ParallelAnimation {
  38. ScriptAction { script: beep.play(); }
  39. PropertyAnimation { properties: "rotation"; duration: 200; }
  40. }
  41. }
  42. ]
  43. }
  44. Button {
  45. anchors.centerIn: parent
  46. text: "Flip!"
  47. onClicked: rectangle.state = rectangle.state === "DEFAULT" ? "REVERSE" : "DEFAULT"
  48. }
  49. }

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.

To do so, we first start by loading our effects:

  1. SoundEffect { id: beep; source: "file:beep.wav"}
  2. SoundEffect { id: swosh; source: "file:swosh.wav" }

Then we define two states for our rectangle, DEFAULT and REVERSE, specifying the expected rotation angle for each state:

  1. states: [
  2. State {
  3. name: "DEFAULT"
  4. PropertyChanges { target: rectangle; rotation: 0; }
  5. },
  6. State {
  7. name: "REVERSE"
  8. PropertyChanges { target: rectangle; rotation: 180; }
  9. }
  10. ]

To provide between-states animation, we define two transitions:

  1. transitions: [
  2. Transition {
  3. to: "DEFAULT"
  4. ParallelAnimation {
  5. ScriptAction { script: swosh.play(); }
  6. PropertyAnimation { properties: "rotation"; duration: 200; }
  7. }
  8. },
  9. Transition {
  10. to: "REVERSE"
  11. ParallelAnimation {
  12. ScriptAction { script: beep.play(); }
  13. PropertyAnimation { properties: "rotation"; duration: 200; }
  14. }
  15. }
  16. ]

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.

TIP

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.

WARNING

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.