QSound Class

The QSound class provides a method to play .wav sound files. More...

Header: #include <QSound>
qmake: QT += multimedia
Inherits: QObject

Public Types

enum Loop { Infinite }

Detailed Description

Qt provides the most commonly required audio operation in GUI applications: asynchronously playing a sound file. This is most easily accomplished using the static play() function:

 QSound::play("mysounds/bells.wav");

Alternatively, create a QSound object from the sound file first and then call the play() slot:

 QSound bells("mysounds/bells.wav");
 bells.play();

In both cases, the file may either be a local file or in a resource.

Once created a QSound object can be queried for its fileName() and total number of loops() (i.e. the number of times the sound will play). The number of repetitions can be altered using the setLoops() function. While playing the sound, the loopsRemaining() function returns the remaining number of repetitions. Use the isFinished() function to determine whether the sound has finished playing.

Sounds played using a QSound object may use more memory than the static play() function, but it may also play more immediately (depending on the underlying platform audio facilities).

If you require finer control over playing sounds, consider the QSoundEffect or QAudioOutput classes.

See also QSoundEffect.

Member Type Documentation

enum QSound::Loop

ConstantValueDescription
QSound::Infinite-1Can be used as a parameter to setLoops() to loop infinitely.