QSound 类

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 = nullptr)
virtual ~QSound ()
QString fileName () const
bool isFinished () const
int loops () const
int loopsRemaining () const
void setLoops (int number )

公共槽

void play ()
void stop ()

静态公共成员

void play (const QString & filename )
const QMetaObject staticMetaObject

额外继承成员

详细描述

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

Qt 提供在 GUI 应用程序中,最常要求的音频操作:异步播放声音文件。这最易施行,使用静态 play () 函数:

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

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

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

在这 2 种情况下,文件要么可以是本地文件,要么可以是在 resource .

Once created a QSound object can be queried for its fileName () 和总数对于 loops () (即:声音将播放的次数)。重复次数的变更可以使用 setLoops () 函数。当播放声音时, loopsRemaining () 函数返回剩余的重复次数。使用 isFinished () 函数,确定声音是否已播放完成。

Sounds played using a QSound object may use more memory than the static play () 函数,但也可能更直接发挥作用 (从属底层平台音频设施)。

若要求更精细控制声音播放,考虑 QSoundEffect or QAudioOutput 类。

另请参阅 QSoundEffect .

成员类型文档编制

enum QSound:: Loop

常量 描述
QSound::Infinite -1 可以作为参数用于 setLoops () 以无限循环。

成员函数文档编制

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

构造 QSound object from the file specified by the given filename 和采用给定 parent .

文件要么可以是本地文件,要么可以是在 resource .

另请参阅 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 () 和 isFinished ().

QString QSound:: fileName () const

返回的文件名关联此 QSound 对象。

另请参阅 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 () 和 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 () 和 isFinished ().

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

播放声音存储在的文件指定通过给定 filename .

文件要么可以是本地文件,要么可以是在 resource .

另请参阅 stop (), loopsRemaining (),和 isFinished ().

[slot] void QSound:: play ()

这是重载函数。

开始播放声音指定通过此 QSound 对象。

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 )

把声音设为重复给定 number 的次数,当播放它时。

注意,传递值 QSound::Infinite 将导致声音无限循环。

另请参阅 loops ().

[slot] void QSound:: stop ()

停止播放声音。

另请参阅 play ().