class QSoundEffect

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

Inheritance diagram of PySide6.QtMultimedia.QSoundEffect

Synopsis

Properties

Methods

Slots

Signals

Static functions

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

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:

effect = QSoundEffect()
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()
    self.m_explosion = self

        m_explosion.setSource(QUrl.fromLocalFile("explosion.wav"))
        m_explosion.setVolume(0.25f)
        # Set up click handling etc.
        clickSource.clicked.connect(m_explosion.play)

# private
    m_explosion = QSoundEffect()

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

class Loop

Constant

Description

QSoundEffect.Infinite

Used as a parameter to setLoopCount() for infinite looping

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

Note

Properties can be used directly when from __feature__ import true_property is used or via accessor functions otherwise.

property audioDeviceᅟ: QAudioDevice

Returns the QAudioDevice instance.

Access functions:
property 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:
property loopsRemainingᅟ: int

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

Access functions:
property mutedᅟ: bool

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

Access functions:
property playingᅟ: bool

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

Access functions:
property 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:
property statusᅟ: QSoundEffect.Status

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

Access functions:
property volumeᅟ: float

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

Access functions:
__init__([parent=None])
Parameters:

parentQObject

Creates a QSoundEffect with the given parent.

__init__(audioDevice[, parent=None])
Parameters:

Creates a QSoundEffect with the given audioDevice and parent.

audioDevice()
Return type:

QAudioDevice

See also

setAudioDevice()

Getter of property audioDeviceᅟ .

audioDeviceChanged()

Notification signal of property audioDeviceᅟ .

isLoaded()
Return type:

bool

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

isMuted()
Return type:

bool

Returns whether this sound effect is muted

Getter of property mutedᅟ .

isPlaying()
Return type:

bool

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

Getter of property playingᅟ .

loadedChanged()

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

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()

Getter of property loopsᅟ .

loopCountChanged()

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

Notification signal of property loopsᅟ .

loopsRemaining()
Return type:

int

Getter of property loopsRemainingᅟ .

loopsRemainingChanged()

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

Notification signal of property loopsRemainingᅟ .

mutedChanged()

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

Notification signal of property mutedᅟ .

play()

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

playingChanged()

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

Notification signal of property playingᅟ .

setAudioDevice(device)
Parameters:

deviceQAudioDevice

See also

audioDevice()

Setter of property audioDeviceᅟ .

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.

Setter of property loopsᅟ .

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()

Setter of property mutedᅟ .

setSource(url)
Parameters:

urlQUrl

Set the current URL to play to url.

See also

source()

Setter of property sourceᅟ .

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()

Setter of property volumeᅟ .

source()
Return type:

QUrl

Returns the URL of the current source to play

See also

setSource()

Getter of property sourceᅟ .

sourceChanged()

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

Notification signal of property sourceᅟ .

status()
Return type:

Status

Returns the current status of this sound effect.

Getter of property statusᅟ .

statusChanged()

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

Notification signal of property statusᅟ .

stop()

Stop current playback.

static supportedMimeTypes()
Return type:

list of strings

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

volume()
Return type:

float

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

See also

setVolume()

Getter of property volumeᅟ .

volumeChanged()

The volumeChanged signal is emitted when the volume has changed.

Notification signal of property volumeᅟ .