- Qt 5.5
- Qt Multimedia
- C++ Classes
- QSoundEffect
- Qt 5.5.1 Reference Documentation

# QSoundEffect Class

The QSoundEffect class provides a way to play low latency sound effects. More...

Header: | #include <QSoundEffect>
qmake: | QT += multimedia
Instantiated By: | SoundEffect
Inherits: |

- List of all members, including inherited members

## Public Types

enum | Loop { Infinite }
enum | Status { Null, Loading, Ready, Error }

## Properties

category : QString loops : int loopsRemaining : const int muted : bool | playing : const bool source : QUrl status : const Status volume : qreal

## Public Functions

| QSoundEffect(QObject * parent = 0)
| ~QSoundEffect()
QString | category() const
bool | isLoaded() const
bool | isMuted() const
bool | isPlaying() const
int | loopCount() const
int | loopsRemaining() const
void | setCategory(const QString & category)
void | setLoopCount(int loopCount)
void | setMuted(bool muted)
void | setSource(const QUrl & url)
void | setVolume(qreal volume)
QUrl | source() const
Status | status() const
qreal | volume() const

## Public Slots

void | play()
void | stop()

## Signals

void | categoryChanged()
void | loadedChanged()
void | loopCountChanged()
void | loopsRemainingChanged()
void | mutedChanged()
void | playingChanged()
void | sourceChanged()
void | statusChanged()
void | volumeChanged()

## Static Public Members

QStringList | supportedMimeTypes()

## Detailed Description

The QSoundEffect class provides a way to play low latency sound effects.

This class 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 QMediaPlayer class instead, since it supports a wider variety of media formats and is less resource intensive.

This example shows how a looping, somewhat quiet sound effect can be played:

    QSoundEffect effect;
    effect.setSource(QUrl::fromLocalFile("engine.wav"));
    effect.setLoopCount(QSoundEffect::Infinite);
    effect.setVolume(0.25f);
    effect.play();

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 assists with lower latency audio playback.

    MyGame()
        : m_explosion(this)
    {
        m_explosion.setSource(QUrl::fromLocalFile("explosion.wav"));
        m_explosion.setVolume(0.25f);

        // Set up click handling etc.
        connect(clickSource, SIGNAL(clicked()), &m_explosion, SLOT(play()));
    }
    private:
    QSoundEffect m_explosion;

Since QSoundEffect requires slightly more resources to achieve lower latency playback, the platform may limit the number of simultaneously playing sound effects.

## Member Type Documentation

### enum QSoundEffect::Loop

Constant | Value | Description
--- | --- | ---
`QSoundEffect::Infinite` | `-2` | Used as a parameter to setLoopCount() for infinite looping

### enum QSoundEffect::Status

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

## Property Documentation

### category : QString

This property contains the category of this sound effect.

Some platforms can perform different audio routing for different categories, or may allow the user to set different volume levels for different categories.

This setting will be ignored on platforms that do not support audio categories.

Access functions:

QString | category() const
void | setCategory(const QString & category)

Notifier signal:

void | categoryChanged()

### 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.

Access functions:

int | loopCount() const
void | setLoopCount(int loopCount)

Notifier signal:

void | loopCountChanged()

### loopsRemaining : const int

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

Access functions:

int | loopsRemaining() const

Notifier signal:

void | loopsRemainingChanged()

### muted : bool

This property provides a way to control muting. A value of `true` will mute this effect.

Access functions:

bool | isMuted() const
void | setMuted(bool muted)

Notifier signal:

void | mutedChanged()

### playing : const bool

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

Access functions:

bool | isPlaying() const

Notifier signal:

void | playingChanged()

### source : QUrl

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.

Access functions:

QUrl | source() const
void | setSource(const QUrl & url)

Notifier signal:

void | sourceChanged()

### status : const Status

This property indicates the current status of the sound effect from the QSoundEffect::Status enumeration.

Access functions:

Status | status() const

Notifier signal:

void | statusChanged()

### volume : qreal

This property holds the volume of the sound effect playback, from 0.0 (silent) to 1.0 (maximum volume).

Access functions:

qreal | volume() const
void | setVolume(qreal volume)

Notifier signal:

void | volumeChanged()

## Member Function Documentation

### QSoundEffect::QSoundEffect(QObject * parent = 0)

Creates a QSoundEffect with the given parent.

### QSoundEffect::~QSoundEffect()

Destroys this sound effect.

### bool QSoundEffect::isLoaded() const

Returns whether the sound effect has finished loading the source().

### `[signal]` void QSoundEffect::loadedChanged()

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

### `[slot]` void QSoundEffect::play()

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

### `[slot]` void QSoundEffect::stop()

Stop current playback.

### `[static]` QStringList QSoundEffect::supportedMimeTypes()

Returns a list of the supported mime types for this platform.