- Qt 6.12
- Qt Multimedia
- QML Types
- SoundEffect
- Qt 6.12.0 Reference Documentation

# SoundEffect QML Type

The SoundEffect type provides a way to play sound effects in QML. More...

Import Statement: | `import QtMultimedia`
In C++: | QSoundEffect

- List of all members, including inherited members

## Properties

- loops : int
- loopsRemaining : int
- muted : bool
- playing : bool
- source : url
- status : enumeration
- volume : real

## Signals

- loadedChanged()
- loopCountChanged()
- loopsRemainingChanged()
- mutedChanged()
- playingChanged()
- sourceChanged()
- statusChanged()
- volumeChanged()

## Methods

- bool isLoaded()
- void play()
- void stop()

## Detailed Description

This type allows you to play uncompressed audio files (typically WAV files) in a generally lower latency way, and is suitable for "feedback" type sounds in response to user actions (e.g. virtual keyboard sounds, positive or negative feedback for popup dialogs, or game sounds). If low latency is not important, consider using the MediaPlayer type instead, since it support a wider variety of media formats and is less resource intensive.

Typically the sound effect should be reused, which allows all the parsing and preparation to be done ahead of time, and only triggered when necessary. This is easy to achieve with QML, since you can declare your SoundEffect instance and refer to it elsewhere.

The following example plays a WAV file on mouse click.

     Text {
         text: "Click Me!";
         font.pointSize: 24;
         width: 150; height: 50;

         SoundEffect {
             id: playSound
             source: "soundeffect.wav"
         }
         MouseArea {
             id: playArea
             anchors.fill: parent
             onPressed: { playSound.play() }
         }
     }

Note: QSoundEffect only supports mono or stereo sound files. Using sound files with a sampling rate of 48000hz is recommended, as this is the typical native sampling rate on most platforms (QAudioDevice::preferredFormat()).

## Property Documentation

### loops : int

This property holds the number of times the sound is played. A value of 0 or 1 means the sound will be played only once; set to SoundEffect.Infinite to enable infinite looping.

The value can be changed while the sound effect is playing, in which case it will update the remaining loops to the new value.

### loopsRemaining : int `[read-only]`

This property contains the number of loops remaining before the sound effect stops by itself, or SoundEffect.Infinite if that's what has been set in loops.

### muted : bool

This property provides a way to control muting. A value of `true` will mute this effect. Otherwise, playback will occur with the currently specified volume.

### playing : bool `[read-only]`

This property indicates whether the sound effect is playing or not.

### source : url

This property holds the url for the sound to play. For the SoundEffect to attempt to load the source, the URL must exist and the application must have read permission in the specified directory. If the desired source is a local file the URL may be specified using either absolute or relative (to the file that declared the SoundEffect) pathing.

### status : enumeration `[read-only]`

This property indicates the current status of the SoundEffect as enumerated within SoundEffect. Possible statuses are listed below.

Value | Description
--- | ---
SoundEffect.Null | No source has been set or the source is null.
SoundEffect.Loading | The SoundEffect is trying to load the source.
SoundEffect.Ready | The source is loaded and ready for play.
SoundEffect.Error | An error occurred during operation, such as failure of loading the source.

### volume : real

This property holds the volume of the sound effect playback.

The volume is scaled linearly from `0.0` (silence) to `1.0` (full volume). Values outside this range will be clamped.

The default volume is `1.0`.

UI volume controls should usually be scaled non-linearly. For example, using a logarithmic scale will produce linear changes in perceived loudness, which is what a user would normally expect from a volume control. See convertVolume() for more details.

## Signal Documentation

### loadedChanged()

The `loadedChanged` signal is emitted when the loading state has changed.

Note: The corresponding handler is `onLoadedChanged`.

### loopCountChanged()

The `loopCountChanged` signal is emitted when the initial number of loops has changed.

Note: The corresponding handler is `onLoopCountChanged`.

### loopsRemainingChanged()

The `loopsRemainingChanged` signal is emitted when the remaining number of loops has changed.

Note: The corresponding handler is `onLoopsRemainingChanged`.

### mutedChanged()

The `mutedChanged` signal is emitted when the mute state has changed.

Note: The corresponding handler is `onMutedChanged`.

### playingChanged()

The `playingChanged` signal is emitted when the playing property has changed.

Note: The corresponding handler is `onPlayingChanged`.

### sourceChanged()

The `sourceChanged` signal is emitted when the source has been changed.

Note: The corresponding handler is `onSourceChanged`.

### statusChanged()

The `statusChanged` signal is emitted when the status property has changed.

Note: The corresponding handler is `onStatusChanged`.

### volumeChanged()

The `volumeChanged` signal is emitted when the volume has changed.

Note: The corresponding handler is `onVolumeChanged`.

## Method Documentation

### bool isLoaded()

Returns whether the sound effect has finished loading the source.

### void play()

Start playback of the sound effect, looping the effect for the number of times as specified in the loops property.

This is the default method for SoundEffect.

     SoundEffect {
         id: playSound
         source: "soundeffect.wav"
     }
     MouseArea {
         id: playArea
         anchors.fill: parent
         onPressed: { playSound.play() }
     }

### void stop()

Stop current playback.