Sound Effects
声音效果
When playing sound effects, the response time from requesting playback until actually playing becomes important. In this situation, the SoundEffect
element comes in handy. By setting up the source
property, a simple call to the play
function immediately starts playback.
播放音效时,从请求播放到实际播放的响应时间变得非常重要。在这种情况下,SoundEffect元素类型就派上了用场。通过设置source属性,对play函数的简单调用立即开始播放。
This can be utilized for audio feedback when tapping the screen, as shown below.
点击屏幕时,这可用于音频反馈,如下所示。
import QtQuick
import QtMultimedia
Window {
width: 300
height: 200
visible: true
SoundEffect {
id: beep
source: Qt.resolvedUrl("beep.wav")
}
Rectangle {
id: button
anchors.centerIn: parent
width: 200
height: 100
color: "red"
MouseArea {
anchors.fill: parent
onClicked: beep.play()
}
}
}
The element can also be utilized to accompany a transition with audio. To trigger playback from a transition, the ScriptAction
element is used.
该元素还可以用于伴随音频的过渡。要从过渡触发播放,请使用ScriptAction元素。
The following example shows how sound effects elements can be used to accompany transition between visual states using animations:
以下示例显示了如何使用声音效果元素来伴随使用动画的视觉状态之间的过渡:
import QtQuick
import QtQuick.Controls
import QtMultimedia
Window {
width: 500
height: 500
visible: true
SoundEffect { id: beep; source: "file:beep.wav"}
SoundEffect { id: swosh; source: "file:swosh.wav" }
Rectangle {
id: rectangle
anchors.centerIn: parent
width: 300
height: width
color: "red"
state: "DEFAULT"
states: [
State {
name: "DEFAULT"
PropertyChanges { target: rectangle; rotation: 0; }
},
State {
name: "REVERSE"
PropertyChanges { target: rectangle; rotation: 180; }
}
]
transitions: [
Transition {
to: "DEFAULT"
ParallelAnimation {
ScriptAction { script: swosh.play(); }
PropertyAnimation { properties: "rotation"; duration: 200; }
}
},
Transition {
to: "REVERSE"
ParallelAnimation {
ScriptAction { script: beep.play(); }
PropertyAnimation { properties: "rotation"; duration: 200; }
}
}
]
}
Button {
anchors.centerIn: parent
text: "Flip!"
onClicked: rectangle.state = rectangle.state === "DEFAULT" ? "REVERSE" : "DEFAULT"
}
}
In this example, we want to apply a 180 rotation animation to our Rectangle
whenever the "Flip!" button is clicked. We also want to play a different sound when the rectangle flips in one direction or the other.
在本例中,我们希望在每次“翻转”时对矩形Rectangle
应用180旋转动画按钮被点击。我们还希望在矩形向一个方向或另一个方向翻转时播放不同的声音。
To do so, we first start by loading our effects:
为此,我们首先从加载效果开始:
SoundEffect { id: beep; source: "file:beep.wav"}
SoundEffect { id: swosh; source: "file:swosh.wav" }
Then we define two states for our rectangle, DEFAULT
and REVERSE
, specifying the expected rotation angle for each state:
然后,我们为矩形定义了两种状态,DEFAULT
和REVERSE
,为每个状态指定预期的旋转角度:
states: [
State {
name: "DEFAULT"
PropertyChanges { target: rectangle; rotation: 0; }
},
State {
name: "REVERSE"
PropertyChanges { target: rectangle; rotation: 180; }
}
]
To provide between-states animation, we define two transitions:
为了提供状态间动画,我们定义了两个过渡:
transitions: [
Transition {
to: "DEFAULT"
ParallelAnimation {
ScriptAction { script: swosh.play(); }
PropertyAnimation { properties: "rotation"; duration: 200; }
}
},
Transition {
to: "REVERSE"
ParallelAnimation {
ScriptAction { script: beep.play(); }
PropertyAnimation { properties: "rotation"; duration: 200; }
}
}
]
Notice the ScriptAction { script: swosh.play(); }
line. Using the ScriptAction
component we can run an arbitrary script as part of the animation, which allows us to play the desired sound effect as part of the animation.
注意ScriptAction { script: swosh.play(); }这行
。使用ScriptAction组件,我们可以运行任意脚本作为动画的一部分,这允许我们在动画中播放所需的声音效果。
TIP
注
In addition to the
play
function, a number of properties similar to the ones offered byMediaPlayer
are available. Examples arevolume
andloops
. The latter can be set toSoundEffect.Infinite
for infinite playback. To stop playback, call thestop
function.除了播放功能
play()
外,还提供了许多类似于MediaPlayer提供的属性。例如音量大小volume
和循环次数loops
。后者可以设置为SoundEffect.Infinite
。要停止播放,请调用停止stop()
功能。
WARNING
警告
When the PulseAudio backend is used,
stop
will not stop instantaneously, but only prevent further loops. This is due to limitations in the underlying API.当使用PulseAudio后端时,stop不会立即停止,只会阻止进一步的循环。这是由于底层API的限制。
示例源码下载