QSoundEffect 类

QSoundEffect 类提供播放低延迟音效的办法。 更多...

头: #include <QSoundEffect>
CMake: find_package(Qt6 COMPONENTS Multimedia REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Multimedia)
qmake: QT += multimedia
实例化: SoundEffect
继承: QObject

公共类型

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

特性

公共函数

  QSoundEffect (const QAudioDevice & audioDevice , QObject * parent = nullptr)
  QSoundEffect (QObject * parent = nullptr)
virtual ~QSoundEffect ()
QAudioDevice audioDevice ()
bool isLoaded () const
bool isMuted () const
bool isPlaying () const
int loopCount () const
int loopsRemaining () const
void setAudioDevice (const QAudioDevice & device )
void setLoopCount (int loopCount )
void setMuted (bool muted )
void setSource (const QUrl & url )
void setVolume (float volume )
QUrl source () const
QSoundEffect::Status status () const
float volume () const

公共槽

void play ()
void stop ()

信号

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

静态公共成员

QStringList supportedMimeTypes ()

详细描述

此类允许以一般更低延迟方式播放未压缩音频文件 (通常是 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, &QPushButton::clicked, &m_explosion, &QSoundEffect::play);
}
private:
QSoundEffect m_explosion;
					

由于 QSoundEffect 要求稍微更多资源以达成更低延迟的回放,因此,平台可能限制同时播放的音效数。

成员类型文档编制

enum QSoundEffect:: Loop

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

enum QSoundEffect:: Status

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

特性文档编制

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

[read-only] 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 ()

[read-only] 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 ()

[read-only] status : const Status

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

访问函数:

QSoundEffect::Status status () const

通知程序信号:

void statusChanged ()

volume : float

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

访问函数:

float volume () const
void setVolume (float volume )

通知程序信号:

void volumeChanged ()

成员函数文档编制

QSoundEffect:: QSoundEffect (const QAudioDevice & audioDevice , QObject * parent = nullptr)

创建 QSoundEffect 采用给定 audioDevice and parent .

QSoundEffect:: QSoundEffect ( QObject * parent = nullptr)

创建 QSoundEffect 采用给定 parent .

[signal] void QSoundEffect:: loadedChanged ()

loadedChanged signal is emitted when the loading state has changed.

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

[signal] void QSoundEffect:: sourceChanged ()

sourceChanged signal is emitted when the source has been changed.

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

[signal] void QSoundEffect:: statusChanged ()

statusChanged signal is emitted when the status property has changed.

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

[slot] void QSoundEffect:: stop ()

Stop current playback.

[signal] void QSoundEffect:: volumeChanged ()

volumeChanged signal is emitted when the volume has changed.

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

[虚拟] QSoundEffect:: ~QSoundEffect ()

Destroys this sound effect.

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 .

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

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 ( float volume )

将音效音量设为 volume .

The volume is scaled linearly from 0.0 (无声) 到 1.0 (full volume). Values outside this range will be clamped.

默认音量为 1.0 .

UI volume controls should usually be scaled non-linearly. 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 ().

QSoundEffect::Status QSoundEffect:: status () const

Returns the current status of this sound effect.

注意: getter 函数对于特性 status。

[static] QStringList QSoundEffect:: supportedMimeTypes ()

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

float QSoundEffect:: volume () const

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

注意: Getter function for property volume.

另请参阅 setVolume ().