QSoundEffect

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

Inheritance diagram of PySide6.QtMultimedia.QSoundEffect

Synopsis

Functions

Static functions

Detailed Description

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, &QPushButton::clicked, &m_explosion, &QSoundEffect::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.

class PySide6.QtMultimedia.QSoundEffect([parent=None])

PySide6.QtMultimedia.QSoundEffect(audioDevice[, parent=None])

Parameters

Creates a QSoundEffect with the given parent.

Creates a QSoundEffect with the given audioDevice and parent.

PySide6.QtMultimedia.QSoundEffect.Loop

Constant

Description

QSoundEffect.Infinite

Used as a parameter to setLoopCount() for infinite looping

PySide6.QtMultimedia.QSoundEffect.Status

Constant

Description

QSoundEffect.Null

No source has been set or the source is null.

QSoundEffect.Loading

The SoundEffect is trying to load the source.

QSoundEffect.Ready

The source is loaded and ready for play.

QSoundEffect.Error

An error occurred during operation, such as failure of loading the source.

PySide6.QtMultimedia.QSoundEffect.audioDevice()
Return type

PySide6.QtMultimedia.QAudioDevice

See also

setAudioDevice()

PySide6.QtMultimedia.QSoundEffect.audioDeviceChanged()
PySide6.QtMultimedia.QSoundEffect.isLoaded()
Return type

bool

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

PySide6.QtMultimedia.QSoundEffect.isMuted()
Return type

bool

Returns whether this sound effect is muted

PySide6.QtMultimedia.QSoundEffect.isPlaying()
Return type

bool

Returns true if the sound effect is currently playing, or false otherwise

PySide6.QtMultimedia.QSoundEffect.loadedChanged()

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

PySide6.QtMultimedia.QSoundEffect.loopCount()
Return type

int

Returns the total number of times that this sound effect will be played before stopping.

See the loopsRemaining() method for the number of loops currently remaining.

See also

setLoopCount()

PySide6.QtMultimedia.QSoundEffect.loopCountChanged()

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

PySide6.QtMultimedia.QSoundEffect.loopsRemaining()
Return type

int

PySide6.QtMultimedia.QSoundEffect.loopsRemainingChanged()

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

PySide6.QtMultimedia.QSoundEffect.mutedChanged()

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

PySide6.QtMultimedia.QSoundEffect.play()

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

PySide6.QtMultimedia.QSoundEffect.playingChanged()

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

PySide6.QtMultimedia.QSoundEffect.setAudioDevice(device)
Parameters

devicePySide6.QtMultimedia.QAudioDevice

See also

audioDevice()

PySide6.QtMultimedia.QSoundEffect.setLoopCount(loopCount)
Parameters

loopCount – int

Set the total number of times to play this sound effect to loopCount.

Setting the loop count to 0 or 1 means the sound effect will be played only once; pass QSoundEffect::Infinite to repeat indefinitely. The loop count can be changed while the sound effect is playing, in which case it will update the remaining loops to the new loopCount.

PySide6.QtMultimedia.QSoundEffect.setMuted(muted)
Parameters

muted – bool

Sets whether to mute this sound effect’s playback.

If muted is true, playback will be muted (silenced), and otherwise playback will occur with the currently specified volume() .

See also

isMuted()

PySide6.QtMultimedia.QSoundEffect.setSource(url)
Parameters

urlPySide6.QtCore.QUrl

Set the current URL to play to url.

See also

source()

PySide6.QtMultimedia.QSoundEffect.setVolume(volume)
Parameters

volume – float

Sets the sound effect volume to volume.

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.

See also

volume()

PySide6.QtMultimedia.QSoundEffect.source()
Return type

PySide6.QtCore.QUrl

Returns the URL of the current source to play

See also

setSource()

PySide6.QtMultimedia.QSoundEffect.sourceChanged()

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

PySide6.QtMultimedia.QSoundEffect.status()
Return type

Status

Returns the current status of this sound effect.

PySide6.QtMultimedia.QSoundEffect.statusChanged()

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

PySide6.QtMultimedia.QSoundEffect.stop()

Stop current playback.

static PySide6.QtMultimedia.QSoundEffect.supportedMimeTypes()
Return type

list of strings

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

PySide6.QtMultimedia.QSoundEffect.volume()
Return type

float

Returns the current volume of this sound effect, from 0.0 (silent) to 1.0 (maximum volume).

See also

setVolume()

PySide6.QtMultimedia.QSoundEffect.volumeChanged()

The volumeChanged signal is emitted when the volume has changed.