SoundEffect QML Type

The SoundEffect type provides a way to play sound effects in QML. 更多...

import 語句: import QtMultimedia 5.12
實例化: QSoundEffect

特性

信號

方法

詳細描述

This type allows you to play uncompressed audio files (typically WAV files) in a generally lower latency way, and is suitable for "feedback" type sounds in response to user actions (e.g. virtual keyboard sounds, positive or negative feedback for popup dialogs, or game sounds). If low latency is not important, consider using the MediaPlayer or Audio types instead, since they support a wider variety of media formats and are less resource intensive.

Typically the sound effect should be reused, which allows all the parsing and preparation to be done ahead of time, and only triggered when necessary. This is easy to achieve with QML, since you can declare your SoundEffect instance and refer to it elsewhere.

The following example plays a WAV file on mouse click.

Text {
    text: "Click Me!";
    font.pointSize: 24;
    width: 150; height: 50;
    SoundEffect {
        id: playSound
        source: "soundeffect.wav"
    }
    MouseArea {
        id: playArea
        anchors.fill: parent
        onPressed: { playSound.play() }
    }
}
					

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

特性文檔編製

category : string

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.

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.

loopsRemaining : int

This property contains the number of loops remaining before the sound effect stops by itself, or SoundEffect .Infinite if that's what has been set in loops .

muted : bool

This property provides a way to control muting. A value of true will mute this effect. Otherwise, playback will occur with the currently specified volume .

playing : bool

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

source : url

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. If the desired source is a local file the URL may be specified using either absolute or relative (to the file that declared the SoundEffect ) pathing.

status : enumeration

This property indicates the current status of the SoundEffect as enumerated within SoundEffect . Possible statuses are listed below.

描述
SoundEffect .Null 未設置源或源為 null。
SoundEffect .Loading The SoundEffect 正試著加載源。
SoundEffect .Ready 源被加載並準備播放。
SoundEffect .Error 操作期間發生錯誤,譬如:源加載失敗。

volume : qreal

This property holds the volume of the sound effect playback.

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

默認音量為 1.0 .

通常,UI 音量控件應按非綫性比例縮放。例如,使用對數比例縮放將産生感知響度的綫性變化,這通常是用戶期望的音量控製。見 QtMultimedia.convertVolume() 瞭解更多細節。


信號文檔編製

categoryChanged ()

The categoryChanged signal is emitted when the category property has changed.

相應處理程序是 onCategoryChanged .

loadedChanged ()

The loadedChanged signal is emitted when the loading state has changed.

相應處理程序是 onLoadedChanged .

loopCountChanged ()

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

相應處理程序是 onLoopCountChanged .

loopsRemainingChanged ()

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

相應處理程序是 onLoopsRemainingChanged .

mutedChanged ()

The mutedChanged signal is emitted when the mute state has changed.

相應處理程序是 onMutedChanged .

playingChanged ()

The playingChanged signal is emitted when the playing property has changed.

相應處理程序是 onPlayingChanged .

sourceChanged ()

The sourceChanged signal is emitted when the source has been changed.

相應處理程序是 onSourceChanged .

statusChanged ()

The statusChanged signal is emitted when the status property has changed.

相應處理程序是 onStatusChanged .

volumeChanged ()

The volumeChanged signal is emitted when the volume has changed.

相應處理程序是 onVolumeChanged .


方法文檔編製

bool isLoaded ()

Returns whether the sound effect has finished loading the source .

play ()

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

This is the default method for SoundEffect .

SoundEffect {
    id: playSound
    source: "soundeffect.wav"
}
MouseArea {
    id: playArea
    anchors.fill: parent
    onPressed: { playSound.play() }
}
																								

stop ()

Stop current playback.