QSoundEffect 類

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

額外繼承成員

詳細描述

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

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

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

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

注意: 通知程序信號對於特性 loops .

[signal] void QSoundEffect:: loopsRemainingChanged ()

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

注意: 通知程序信號對於特性 loopsRemaining .

[signal] void QSoundEffect:: mutedChanged ()

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

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

按綫性比例縮放的音量是從 0.0 (無聲) 到 1.0 (全音量)。將鉗製超齣此範圍的值。

默認音量為 1.0 .

通常,UI 音量控件應按非綫性比例縮放。例如,使用對數比例縮放將産生感知響度的綫性變化,這通常是用戶期望的音量控製。見 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 ()

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

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

The volumeChanged signal is emitted when the volume has changed.

注意: 通知程序信號對於特性 volume .