QSoundEffect 类提供播放低延迟音效的办法。 更多...
头: | #include <QSoundEffect> |
CMake: |
find_package(Qt6 REQUIRED COMPONENTS Multimedia)
target_link_libraries(mytarget PRIVATE Qt6::Multimedia) |
qmake: | QT += multimedia |
实例化: | SoundEffect |
继承: | QObject |
enum | Loop { Infinite } |
enum | Status { Null, Loading, Ready, Error } |
|
QSoundEffect (QObject * parent = nullptr) | |
QSoundEffect (const QAudioDevice & audioDevice , 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 要求稍微更多资源以达成更低延迟的回放,因此,平台可能限制同时播放的音效数。
常量 | 值 | 描述 |
---|---|---|
QSoundEffect::Infinite
|
-2
|
作为参数用于 setLoopCount () 为无限循环 |
常量 | 值 | 描述 |
---|---|---|
QSoundEffect::Null
|
0
|
未设置源或源为 null。 |
QSoundEffect::Loading
|
1
|
The SoundEffect 正试着加载源。 |
QSoundEffect::Ready
|
2
|
源被加载并准备播放。 |
QSoundEffect::Error
|
3
|
操作期间发生错误,譬如:源加载失败。 |
返回 QAudioDevice 实例。
访问函数:
QAudioDevice | audioDevice () |
void | setAudioDevice (const QAudioDevice & device ) |
通知程序信号:
void | audioDeviceChanged () |
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 () |
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 () |
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 () |
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 () |
[explicit]
QSoundEffect::
QSoundEffect
(
QObject
*
parent
= nullptr)
创建 QSoundEffect 采用给定 parent .
[explicit]
QSoundEffect::
QSoundEffect
(const
QAudioDevice
&
audioDevice
,
QObject
*
parent
= nullptr)
创建 QSoundEffect 采用给定 audioDevice and parent .
[virtual noexcept]
QSoundEffect::
~QSoundEffect
()
Destroys this sound effect.
Returns whether the sound effect has finished loading the source ().
Returns whether this sound effect is muted
注意: getter 函数对于特性 muted .
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.
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 .
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 ().
设置是否静音此音效的回放。
若 muted is true, playback will be muted (silenced), and otherwise playback will occur with the currently specified volume ().
注意: setter 函数对于特性 muted .
另请参阅 isMuted ().
Set the current URL to play to url .
注意: setter 函数对于特性 source .
另请参阅 source ().
将音效音量设为 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 QtAudio::convertVolume () 了解更多细节。
注意: setter 函数对于特性 volume .
另请参阅 volume ().
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 .
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.
Returns the current volume of this sound effect, from 0.0 (silent) to 1.0 (maximum volume).
注意: Getter function for property volume.
另请参阅 setVolume ().
[signal]
void
QSoundEffect::
volumeChanged
()
The
volumeChanged
signal is emitted when the volume has changed.
注意: 通知程序信号对于特性 volume .