当播放声音效果时,从请求播放到真实响应播放的响应时间非常重要
一
import QtQuick 2.9
import QtQuick.Window 2.2
import QtMultimedia 5.0
Window {
visible: true
width: 770
height: 480
title: qsTr("Hello World")
Item {
id: root
anchors.fill: parent
SoundEffect{
id:beep
source:"file:///E:/QT_Object/"
+ "Multimedia_2/beep.wav"
}
// button to play the wav
Rectangle{
id:button
anchors.centerIn: parent
width: 200
height: 100
color: "red"
MouseArea{
anchors.fill: parent
onClicked: beep.play()
}
}
}
}
当点击按钮会播放音乐!
二
import QtQuick 2.9
import QtQuick.Window 2.2
import QtMultimedia 5.0
Window {
visible: true
width: 770
height: 480
title: qsTr("Hello World")
SoundEffect {
id: beep
source: "file:///E:/QT_Object/"
+ "Multimedia_2/beep.wav"
}
SoundEffect {
id: swosh
source: "file:///E:/QT_Object/"
+ "Multimedia_2/swosh.wav"
}
// 播放 beep.wav 的button
Rectangle {
id: button_beep
anchors.centerIn: parent
width: 200
height: 100
color: "red"
MouseArea {
anchors.fill: parent
onClicked: beep.play()
}
Text {
anchors.centerIn: parent
text: qsTr("按下播放音乐")
}
}
// 状态转变时播放音乐的 button
Item {
id: root
width: 200
height: 100
anchors.top: button_beep.bottom
anchors.topMargin: 20
anchors.left: button_beep.left
Rectangle{
anchors.fill: parent
color: "blue"
Text {
anchors.centerIn: parent
text: qsTr("按下播放音乐并旋转")
}
}
// 点击按钮触发状态改变
MouseArea {
anchors.fill: parent
onClicked: {
if (root.state === "upsidedown")
root.state = ""
else
root.state = "upsidedown"
}
}
// 状态
states: [
State {
name: "upsidedown"
PropertyChanges {
target: root
rotation: 360
}
}
]
// 状态时的动画
transitions: [
Transition {
// 平行动画组
ParallelAnimation {
// 动画1:播放音乐
ScriptAction {
script: swosh.play()
}
// 动画2:旋转按钮
PropertyAnimation {
properties: "rotation"
duration: 200
}
}
}
]
}
}
点击蓝色按钮,旋转 360 度的同时播放音乐!