Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QSoundEffect Class Reference

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

#include <qsoundeffect.h>

+ Inheritance diagram for QSoundEffect:
+ Collaboration diagram for QSoundEffect:

Public Types

enum  Loop { Infinite = -2 }
 \value Infinite Used as a parameter to \l setLoopCount() for infinite looping More...
 
enum  Status { Null , Loading , Ready , Error }
 \value Null No source has been set or the source is null. More...
 

Public Slots

void play ()
 \qmlmethod QtMultimedia::SoundEffect::play()
 
void stop ()
 \qmlmethod QtMultimedia::SoundEffect::stop()
 
- Public Slots inherited from QObject
void deleteLater ()
 \threadsafe
 

Signals

void sourceChanged ()
 The sourceChanged signal is emitted when the source has been changed.
 
void loopCountChanged ()
 \qmlsignal QtMultimedia::SoundEffect::loadedChanged()
 
void loopsRemainingChanged ()
 \qmlsignal QtMultimedia::SoundEffect::loopCountChanged()
 
void volumeChanged ()
 \qmlsignal QtMultimedia::SoundEffect::loopsRemainingChanged()
 
void mutedChanged ()
 \qmlsignal QtMultimedia::SoundEffect::volumeChanged()
 
void loadedChanged ()
 \qmlsignal QtMultimedia::SoundEffect::sourceChanged()
 
void playingChanged ()
 \qmlsignal QtMultimedia::SoundEffect::mutedChanged()
 
void statusChanged ()
 \qmlsignal QtMultimedia::SoundEffect::playingChanged()
 
void audioDeviceChanged ()
 
- Signals inherited from QObject
void destroyed (QObject *=nullptr)
 This signal is emitted immediately before the object obj is destroyed, after any instances of QPointer have been notified, and cannot be blocked.
 
void objectNameChanged (const QString &objectName, QPrivateSignal)
 This signal is emitted after the object's name has been changed.
 

Public Member Functions

 QSoundEffect (QObject *parent=nullptr)
 \qmltype SoundEffect \nativetype QSoundEffect
 
 QSoundEffect (const QAudioDevice &audioDevice, QObject *parent=nullptr)
 Creates a QSoundEffect with the given audioDevice and parent.
 
 ~QSoundEffect ()
 Destroys this sound effect.
 
QUrl source () const
 Returns the URL of the current source to play.
 
void setSource (const QUrl &url)
 Set the current URL to play to url.
 
int loopCount () const
 Returns the total number of times that this sound effect will be played before stopping.
 
int loopsRemaining () const
 
void setLoopCount (int loopCount)
 Set the total number of times to play this sound effect to loopCount.
 
QAudioDevice audioDevice ()
 
void setAudioDevice (const QAudioDevice &device)
 
float volume () const
 Returns the current volume of this sound effect, from 0.0 (silent) to 1.0 (maximum volume).
 
void setVolume (float volume)
 Sets the sound effect volume to volume.
 
bool isMuted () const
 Returns whether this sound effect is muted.
 
void setMuted (bool muted)
 Sets whether to mute this sound effect's playback.
 
bool isLoaded () const
 Returns whether the sound effect has finished loading the \l source().
 
bool isPlaying () const
 Returns true if the sound effect is currently playing, or false otherwise.
 
Status status () const
 Returns the current status of this sound effect.
 
- Public Member Functions inherited from QObject
Q_INVOKABLE QObject (QObject *parent=nullptr)
 Constructs an object with parent object parent.
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects.
 
virtual bool event (QEvent *event)
 This virtual function receives events to an object and should return true if the event e was recognized and processed.
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 Filters events if this object has been installed as an event filter for the watched object.
 
QString objectName () const
 
Q_WEAK_OVERLOAD void setObjectName (const QString &name)
 Sets the object's name to name.
 
void setObjectName (QAnyStringView name)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QBindable< QStringbindableObjectName ()
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false.
 
bool isWindowType () const
 Returns true if the object is a window; otherwise returns false.
 
bool isQuickItemType () const
 Returns true if the object is a QQuickItem; otherwise returns false.
 
bool signalsBlocked () const noexcept
 Returns true if signals are blocked; otherwise returns false.
 
bool blockSignals (bool b) noexcept
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it).
 
QThreadthread () const
 Returns the thread in which the object lives.
 
bool moveToThread (QThread *thread QT6_DECL_NEW_OVERLOAD_TAIL)
 Changes the thread affinity for this object and its children and returns true on success.
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 This is an overloaded function that will start a timer of type timerType and a timeout of interval milliseconds.
 
int startTimer (std::chrono::nanoseconds time, Qt::TimerType timerType=Qt::CoarseTimer)
 
void killTimer (int id)
 Kills the timer with timer identifier, id.
 
void killTimer (Qt::TimerId id)
 
template<typename T >
findChild (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns the child of this object that can be cast into type T and that is called name, or \nullptr if there is no such object.
 
template<typename T >
QList< T > findChildren (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects.
 
template<typename T >
findChild (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<typename T >
QList< T > findChildren (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
const QObjectListchildren () const
 Returns a list of child objects.
 
void setParent (QObject *parent)
 Makes the object a child of parent.
 
void installEventFilter (QObject *filterObj)
 Installs an event filter filterObj on this object.
 
void removeEventFilter (QObject *obj)
 Removes an event filter object obj from this object.
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=nullptr, const QObject *receiver=nullptr, const char *member=nullptr) const
 
bool disconnect (const QObject *receiver, const char *member=nullptr) const
 
void dumpObjectTree () const
 Dumps a tree of children to the debug output.
 
void dumpObjectInfo () const
 Dumps information about signal connections, etc.
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value.
 
bool setProperty (const char *name, QVariant &&value)
 
QVariant property (const char *name) const
 Returns the value of the object's name property.
 
QList< QByteArraydynamicPropertyNames () const
 
QBindingStoragebindingStorage ()
 
const QBindingStoragebindingStorage () const
 
QObjectparent () const
 Returns a pointer to the parent object.
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false.
 

Static Public Member Functions

static QStringList supportedMimeTypes ()
 Returns a list of the supported mime types for this platform.
 
- Static Public Member Functions inherited from QObject
static QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 \threadsafe
 
static QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::ContextTypeForFunctor< Func2 >::ContextType *context, Func2 &&slot, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 &&slot)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 \threadsafe
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static bool disconnect (const QMetaObject::Connection &)
 Disconnect a connection.
 
template<typename Func1 , typename Func2 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot)
 
template<typename Func1 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *receiver, void **zero)
 

Properties

QUrl source
 \qmlproperty url QtMultimedia::SoundEffect::source
 
int loops
 \qmlproperty int QtMultimedia::SoundEffect::loops
 
int loopsRemaining
 \qmlproperty int QtMultimedia::SoundEffect::loopsRemaining
 
float volume
 \qmlproperty qreal QtMultimedia::SoundEffect::volume
 
bool muted
 \qmlproperty bool QtMultimedia::SoundEffect::muted
 
bool playing
 \qmlproperty bool QtMultimedia::SoundEffect::playing
 
Status status
 \qmlproperty enumeration QtMultimedia::SoundEffect::status
 
QAudioDevice audioDevice
 Returns the QAudioDevice instance.
 
- Properties inherited from QObject
QString objectName
 the name of this object
 

Additional Inherited Members

- Protected Member Functions inherited from QObject
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns \nullptr.
 
int senderSignalIndex () const
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal.
 
bool isSignalConnected (const QMetaMethod &signal) const
 
virtual void timerEvent (QTimerEvent *event)
 This event handler can be reimplemented in a subclass to receive timer events for the object.
 
virtual void childEvent (QChildEvent *event)
 This event handler can be reimplemented in a subclass to receive child events.
 
virtual void customEvent (QEvent *event)
 This event handler can be reimplemented in a subclass to receive custom events.
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
 QObject (QObjectPrivate &dd, QObject *parent=nullptr)
 
- Protected Attributes inherited from QObject
QScopedPointer< QObjectDatad_ptr
 

Detailed Description

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

\inmodule QtMultimedia

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:

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.

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

Definition at line 20 of file qsoundeffect.h.

Member Enumeration Documentation

◆ Loop

\value Infinite Used as a parameter to \l setLoopCount() for infinite looping

Enumerator
Infinite 

Definition at line 34 of file qsoundeffect.h.

◆ Status

\value Null No source has been set or the source is null.

\value Loading The SoundEffect is trying to load the source. \value Ready The source is loaded and ready for play. \value Error An error occurred during operation, such as failure of loading the source.

Enumerator
Null 
Loading 
Ready 
Error 

Definition at line 40 of file qsoundeffect.h.

Constructor & Destructor Documentation

◆ QSoundEffect() [1/2]

QSoundEffect::QSoundEffect ( QObject * parent = nullptr)
explicit

\qmltype SoundEffect \nativetype QSoundEffect

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

\inmodule QtMultimedia

\inqmlmodule QtMultimedia

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.

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

Creates a QSoundEffect with the given parent.

Definition at line 354 of file qsoundeffect.cpp.

◆ QSoundEffect() [2/2]

QSoundEffect::QSoundEffect ( const QAudioDevice & audioDevice,
QObject * parent = nullptr )
explicit

Creates a QSoundEffect with the given audioDevice and parent.

Definition at line 362 of file qsoundeffect.cpp.

◆ ~QSoundEffect()

QSoundEffect::~QSoundEffect ( )

Destroys this sound effect.

Definition at line 371 of file qsoundeffect.cpp.

Member Function Documentation

◆ audioDevice()

QAudioDevice QSoundEffect::audioDevice ( )

◆ audioDeviceChanged

void QSoundEffect::audioDeviceChanged ( )
signal

◆ isLoaded()

bool QSoundEffect::isLoaded ( ) const

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

\qmlmethod bool QtMultimedia::SoundEffect::isLoaded()

Returns whether the sound effect has finished loading the \l source.

Definition at line 699 of file qsoundeffect.cpp.

◆ isMuted()

bool QSoundEffect::isMuted ( ) const

Returns whether this sound effect is muted.

Definition at line 663 of file qsoundeffect.cpp.

◆ isPlaying()

bool QSoundEffect::isPlaying ( ) const

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

Definition at line 744 of file qsoundeffect.cpp.

◆ loadedChanged

void QSoundEffect::loadedChanged ( )
signal

\qmlsignal QtMultimedia::SoundEffect::sourceChanged()

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

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

◆ loopCount()

int QSoundEffect::loopCount ( ) const

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

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

Definition at line 499 of file qsoundeffect.cpp.

◆ loopCountChanged

void QSoundEffect::loopCountChanged ( )
signal

\qmlsignal QtMultimedia::SoundEffect::loadedChanged()

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

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

◆ loopsRemaining()

int QSoundEffect::loopsRemaining ( ) const

Definition at line 586 of file qsoundeffect.cpp.

◆ loopsRemainingChanged

void QSoundEffect::loopsRemainingChanged ( )
signal

\qmlsignal QtMultimedia::SoundEffect::loopCountChanged()

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

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

◆ mutedChanged

void QSoundEffect::mutedChanged ( )
signal

\qmlsignal QtMultimedia::SoundEffect::volumeChanged()

The volumeChanged signal is emitted when the volume has changed.

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

◆ play

void QSoundEffect::play ( )
slot

\qmlmethod QtMultimedia::SoundEffect::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.

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

Definition at line 720 of file qsoundeffect.cpp.

◆ playingChanged

void QSoundEffect::playingChanged ( )
signal

\qmlsignal QtMultimedia::SoundEffect::mutedChanged()

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

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

◆ setAudioDevice()

void QSoundEffect::setAudioDevice ( const QAudioDevice & device)

Definition at line 547 of file qsoundeffect.cpp.

◆ setLoopCount()

void QSoundEffect::setLoopCount ( int loopCount)

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.

See also
loopsRemaining()

Definition at line 520 of file qsoundeffect.cpp.

◆ setMuted()

void QSoundEffect::setMuted ( bool muted)

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

Definition at line 675 of file qsoundeffect.cpp.

◆ setSource()

void QSoundEffect::setSource ( const QUrl & url)

Set the current URL to play to url.

Definition at line 420 of file qsoundeffect.cpp.

◆ setVolume()

void QSoundEffect::setVolume ( float volume)

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 QtAudio::convertVolume() for more details.

Definition at line 636 of file qsoundeffect.cpp.

◆ source()

QUrl QSoundEffect::source ( ) const

Returns the URL of the current source to play.

Definition at line 414 of file qsoundeffect.cpp.

◆ sourceChanged

void QSoundEffect::sourceChanged ( )
signal

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

◆ status()

QSoundEffect::Status QSoundEffect::status ( ) const

Returns the current status of this sound effect.

Definition at line 784 of file qsoundeffect.cpp.

◆ statusChanged

void QSoundEffect::statusChanged ( )
signal

\qmlsignal QtMultimedia::SoundEffect::playingChanged()

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

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

◆ stop

void QSoundEffect::stop ( )
slot

\qmlmethod QtMultimedia::SoundEffect::stop()

Stop current playback.

Stop current playback.

Definition at line 801 of file qsoundeffect.cpp.

◆ supportedMimeTypes()

QStringList QSoundEffect::supportedMimeTypes ( )
static

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

Definition at line 384 of file qsoundeffect.cpp.

◆ volume()

float QSoundEffect::volume ( ) const

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

Definition at line 616 of file qsoundeffect.cpp.

◆ volumeChanged

void QSoundEffect::volumeChanged ( )
signal

\qmlsignal QtMultimedia::SoundEffect::loopsRemainingChanged()

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

The volumeChanged signal is emitted when the volume has changed.

Property Documentation

◆ audioDevice

QAudioDevice QSoundEffect::audioDevice
readwrite

Returns the QAudioDevice instance.

Definition at line 31 of file qsoundeffect.h.

◆ loops

int QSoundEffect::loops
readwrite

\qmlproperty int QtMultimedia::SoundEffect::loops

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.

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.

Definition at line 25 of file qsoundeffect.h.

◆ loopsRemaining

int QSoundEffect::loopsRemaining
read

\qmlproperty int QtMultimedia::SoundEffect::loopsRemaining

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 \l loops.

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 \l loops.

Definition at line 26 of file qsoundeffect.h.

◆ muted

bool QSoundEffect::muted
readwrite

\qmlproperty bool QtMultimedia::SoundEffect::muted

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

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

Definition at line 28 of file qsoundeffect.h.

◆ playing

bool QSoundEffect::playing
read

\qmlproperty bool QtMultimedia::SoundEffect::playing

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

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

Definition at line 29 of file qsoundeffect.h.

◆ source

QUrl QSoundEffect::source
readwrite

\qmlproperty url QtMultimedia::SoundEffect::source

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.

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.

Definition at line 24 of file qsoundeffect.h.

◆ status

Status QSoundEffect::status
read

\qmlproperty enumeration QtMultimedia::SoundEffect::status

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

\table \header

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

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

Definition at line 30 of file qsoundeffect.h.

◆ volume

float QSoundEffect::volume
readwrite

\qmlproperty qreal QtMultimedia::SoundEffect::volume

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 \l {QtAudio::convertVolume()}{convertVolume()} for more details.

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

Definition at line 27 of file qsoundeffect.h.


The documentation for this class was generated from the following files: