播放声音效果时,从请求播放到实际播放的响应时间变得非常重要,在这种情况下,SourceEffect元素非常重要,通过设置源属性,对play函数的简单调用立即
开始播放。
import QtQuick
import QtQuick.Window
import QtMultimedia
import QtQuick.Controls
Window {
width: 500
height: 500
visible: true
SoundEffect{id:mine;source:"../images/mine.wav";volume:0.5}
SoundEffect{id:machineGun;source:"../images/machineGun.wav";loops:2}
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:170
}
}
]
transitions:[
Transition{
to:"DEFAULT"
ParallelAnimation{
ScriptAction{script: machineGun.play()}
PropertyAnimation{
properties: "rotation"
duration:200
}
}
},
Transition{
to:"REVERSE"
ParallelAnimation{
ScriptAction{script: mine.play()}
PropertyAnimation{
properties: "rotation"
duration:200
}
}
}
]
}
Button{
anchors.centerIn: parent
text:"Flip"
onClicked:rectangle.state = rectangle.state === "DEFAULT" ?"REVERSE":"DEFAULT"
}
}
显示效果