QSoundEffect 类

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

头: #include <QSoundEffect>
qmake: QT += multimedia
实例化: SoundEffect
继承: QObject

公共类型

enum Loop { Infinite }
enum Status { Null, Loading, Ready, Error }

特性

公共函数

QSoundEffect (QObject * parent = Q_NULLPTR)
~QSoundEffect ()
QString category () const
bool isLoaded () const
bool isMuted () const
bool isPlaying () const
int loopCount () const
int loopsRemaining () const
void setCategory (const QString & category )
void setLoopCount (int loopCount )
void setMuted (bool muted )
void setSource (const QUrl & url )
void setVolume (qreal volume )
QUrl source () const
Status status () const
qreal volume () const

公共槽

void play ()
void stop ()

信号

void categoryChanged ()
void loadedChanged ()
void loopCountChanged ()
void loopsRemainingChanged ()
void mutedChanged ()
void playingChanged ()
void sourceChanged ()
void statusChanged ()
void volumeChanged ()

静态公共成员

QStringList supportedMimeTypes ()

额外继承成员

详细描述

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

此类允许以一般更低延迟方式播放未压缩音频文件 (通常是 WAV 文件),和播放适合响应用户动作的反馈类型的声音 (如:虚拟键盘声音、弹出对话框的正反馈或负反馈、或游戏声音)。若低延迟不重要,考虑使用 QMediaPlayer 类代替,由于它支持的媒体格式更广泛,且资源密集度较低。

此范例展示如何循环播放、有点安静的音效:

QSoundEffect effect;
effect.setSource(QUrl::fromLocalFile("engine.wav"));
effect.setLoopCount(QSoundEffect::Infinite);
effect.setVolume(0.25f);
effect.play();
					

通常应重用音效,这样可以提前完成所有剖析和准备,及触发当有必要时。这有助于更低延迟的音频回放。

MyGame()
    : m_explosion(this)
{
    m_explosion.setSource(QUrl::fromLocalFile("explosion.wav"));
    m_explosion.setVolume(0.25f);
    // Set up click handling etc.
    connect(clickSource, SIGNAL(clicked()), &m_explosion, SLOT(play()));
}
private:
QSoundEffect m_explosion;
					

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

成员类型文档编制

enum QSoundEffect:: Loop

常量 描述
QSoundEffect::Infinite -2 作为参数用于 setLoopCount () 为无限循环

enum QSoundEffect:: Status

常量 描述
QSoundEffect::Null 0 未设置源或源为 null。
QSoundEffect::Loading 1 SoundEffect 正试着加载源。
QSoundEffect::Ready 2 源被加载并准备播放。
QSoundEffect::Error 3 操作期间发生错误,譬如:源加载失败。

特性文档编制

category : QString

This property contains the category of this sound effect.

Some platforms can perform different audio routing for different categories, or may allow the user to set different volume levels for different categories.

This setting will be ignored on platforms that do not support audio categories.

访问函数:

QString category () const
void setCategory (const QString & category )

通知程序信号:

void categoryChanged ()

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.

访问函数:

int loopCount () const
void setLoopCount (int loopCount )

通知程序信号:

void loopCountChanged ()

loopsRemaining : const int

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

访问函数:

int loopsRemaining () const

通知程序信号:

void loopsRemainingChanged ()

muted : bool

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

访问函数:

bool isMuted () const
void setMuted (bool muted )

通知程序信号:

void mutedChanged ()

playing : const bool

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

访问函数:

bool isPlaying () const

通知程序信号:

void playingChanged ()

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.

访问函数:

QUrl source () const
void setSource (const QUrl & url )

通知程序信号:

void sourceChanged ()

status : const Status

This property indicates the current status of the sound effect from the QSoundEffect::Status 枚举。

访问函数:

Status status () const

通知程序信号:

void statusChanged ()

volume : qreal

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

访问函数:

qreal volume () const
void setVolume (qreal volume )

通知程序信号:

void volumeChanged ()

成员函数文档编制

QSoundEffect:: QSoundEffect ( QObject * parent = Q_NULLPTR)

创建 QSoundEffect 采用给定 parent .

QSoundEffect:: ~QSoundEffect ()

Destroys this sound effect.

QString QSoundEffect:: category () const

返回当前 category for this sound effect.

Some platforms can perform different audio routing for different categories, or may allow the user to set different volume levels for different categories.

This setting will be ignored on platforms that do not support audio categories.

注意: Getter 函数对于特性 category .

另请参阅 setCategory ().

[signal] void QSoundEffect:: categoryChanged ()

categoryChanged signal is emitted when the category property has changed.

注意: 通知程序信号对于特性 category .

bool QSoundEffect:: isLoaded () const

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

bool QSoundEffect:: isMuted () const

Returns whether this sound effect is muted

注意: Getter 函数对于特性 muted .

bool QSoundEffect:: isPlaying () const

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

注意: Getter 函数对于特性 playing .

[signal] void QSoundEffect:: loadedChanged ()

loadedChanged signal is emitted when the loading state has changed.

int QSoundEffect:: loopCount () const

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

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

注意: Getter 函数对于特性 loops .

另请参阅 setLoopCount ().

[signal] void QSoundEffect:: loopCountChanged ()

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

注意: 通知程序信号对于特性 loops .

[signal] void QSoundEffect:: loopsRemainingChanged ()

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

注意: 通知程序信号对于特性 loopsRemaining .

[signal] void QSoundEffect:: mutedChanged ()

mutedChanged signal is emitted when the mute state has changed.

注意: 通知程序信号对于特性 muted .

[slot] void QSoundEffect:: play ()

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

[signal] void QSoundEffect:: playingChanged ()

playingChanged signal is emitted when the playing property has changed.

注意: 通知程序信号对于特性 playing .

void QSoundEffect:: setCategory (const QString & category )

设置 category of this sound effect to category .

Some platforms can perform different audio routing for different categories, or may allow the user to set different volume levels for different categories.

This setting will be ignored on platforms that do not support audio categories.

If this setting is changed while a sound effect is playing it will only take effect when the sound effect has stopped playing.

注意: Setter 函数对于特性 category .

另请参阅 category ().

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 .

注意: Setter 函数对于特性 loops .

另请参阅 loopCount () 和 loopsRemaining ().

void QSoundEffect:: setMuted ( bool muted )

设置是否静音此音效的回放。

muted is true, playback will be muted (silenced), and otherwise playback will occur with the currently specified volume ().

注意: Setter 函数对于特性 muted .

另请参阅 isMuted ().

void QSoundEffect:: setSource (const QUrl & url )

Set the current URL to play to url .

注意: Setter 函数对于特性 source .

另请参阅 source ().

void QSoundEffect:: setVolume ( qreal volume )

将音效音量设为 volume .

The volume is scaled linearly from 0.0 (silence) to 1.0 (full volume). Values outside this range will be clamped.

默认音量为 1.0 .

UI volume controls should usually be scaled nonlinearly. 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 QAudio::convertVolume () 了解更多细节。

注意: Setter 函数对于特性 volume .

另请参阅 volume ().

QUrl QSoundEffect:: source () const

Returns the URL of the current source to play

注意: Getter 函数对于特性 source .

另请参阅 setSource ().

[signal] void QSoundEffect:: sourceChanged ()

sourceChanged signal is emitted when the source has been changed.

注意: 通知程序信号对于特性 source .

Status QSoundEffect:: status () const

Returns the current status of this sound effect.

注意: Getter 函数对于特性 status .

[signal] void QSoundEffect:: statusChanged ()

statusChanged signal is emitted when the status property has changed.

注意: 通知程序信号对于特性 status .

[slot] void QSoundEffect:: stop ()

Stop current playback.

[static] QStringList QSoundEffect:: supportedMimeTypes ()

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

qreal QSoundEffect:: volume () const

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

注意: Getter 函数对于特性 volume .

另请参阅 setVolume ().

[signal] void QSoundEffect:: volumeChanged ()

volumeChanged signal is emitted when the volume has changed.

注意: 通知程序信号对于特性 volume .