QSound Class

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

头文件: #include <QSound>
qmake: QT += multimedia
基类: QObject

公有类型

enum Loop { Infinite }

公有函数

QSound(const QString &filename, QObject *parent = Q_NULLPTR)
~QSound()
QString fileName() const
bool isFinished() const
int loops() const
int loopsRemaining() const
void setLoops(int number)
  • 32 个公有函数继承自 QObject

公有槽函数

void play()
void stop()
  • 1 个公有槽函数继承自 QObject

静态公有成员

void play(const QString &filename)
  • 10 个静态公有成员继承自 QObject

其他继承的成员

  • 1 个属性继承自 QObject
  • 2 个信号继承自 QObject
  • 1 个公有变量继承自 QObject
  • 9 个受保护的函数继承自 QObject
  • 2 个受保护的变量继承自 QObject

详细描述

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

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

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.

参见 QSoundEffect.

成员类型

enum QSound::Loop

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

成员函数

QSound::QSound(const QString &filename, QObject *parent = Q_NULLPTR)

Constructs a QSound object from the file specified by the given filename and with the given parent.

参见 play().

QSound::~QSound()

Destroys this sound object. If the sound is not finished playing, the stop() function is called before the sound object is destroyed.

参见 stop() and isFinished().

QString QSound::fileName() const

Returns the filename associated with this QSound object.

参见 QSound().

bool QSound::isFinished() const

Returns true if the sound has finished playing; otherwise returns false.

int QSound::loops() const

Returns the number of times the sound will play. Return value of QSound::Infinite indicates infinite number of loops

参见 loopsRemaining() and setLoops().

int QSound::loopsRemaining() const

Returns the remaining number of times the sound will loop (for all positive values this value decreases each time the sound is played). Return value of QSound::Infinite indicates infinite number of loops

参见 loops() and isFinished().

[static] void QSound::play(const QString &filename)

Plays the sound stored in the file specified by the given filename.

参见 stop(), loopsRemaining(), and isFinished().

[slot] void QSound::play()

This is an overloaded function.

Starts playing the sound specified by this QSound object.

The function returns immediately. Depending on the platform audio facilities, other sounds may stop or be mixed with the new sound. The sound can be played again at any time, possibly mixing or replacing previous plays of the sound.

参见 fileName().

void QSound::setLoops(int number)

Sets the sound to repeat the given number of times when it is played.

Note that passing the value QSound::Infinite will cause the sound to loop indefinitely.

参见 loops().

[slot] void QSound::stop()

Stops the sound playing.

参见 play().