# 声音特效

在播放音效时,从请求播放到实际播放的响应时间变得很重要。在这种情况下,SoundEffect元素就派上用场了。通过设置source属性,简单调用play函数会立即开始播放。

如下所示,这可用作点击屏幕时的音频反馈。

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()
        }
    }
}

该元素还可用于带有伴随音频的过渡。要从过渡触发播放,使用ScriptAction元素。

以下示例显示了如何使用音效元素来伴随使用动画的视觉状态之间的转换:

import QtQuick
import QtQuick.Controls
import QtMultimedia

Window {
    width: 500
    height: 500
    visible: true

    SoundEffect { id: beep; source: Qt.resolvedUrl("beep.wav")}
    SoundEffect { id: swosh; source: Qt.resolvedUrl("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"
    }
}

译者注释

原始的文章中使用的资源地址是:

    SoundEffect { id: beep; source: "file:beep.wav"}
    SoundEffect { id: swosh; source: "file:swosh.wav" }

执行上面的代码会报错:

QSoundEffect(qaudio): Error decoding source file:beep.wav
QSoundEffect(qaudio): Error decoding source file:swosh.wav

我查阅资料,上面有些说确实是文件的编码有问题回报这个错误。但是在上面的一个示例中,使用url的其他语句进行路径的解析是没有问题的。
我修改成上面使用的Qt.resolvedUrl("beep.wav")就能够使用。

在这个例子中,希望在点击“Flip!”(翻转)按钮时对Rectangle(矩形)应用180度的旋转动画。当矩形向不同方向翻转时,想要播放不同的声音。

为此,首先加载特效:

SoundEffect { id: beep; source: Qt.resolvedUrl("beep.wav")}
SoundEffect { id: swosh; source: Qt.resolvedUrl("swosh.wav") }

然后为矩形定义两个状态,DEFAULTREVERSE,为每个状态指定预期的旋转角度:

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; }
        }
    }
]

注意ScriptAction { script: swosh.play(); }行。 使用了ScriptAction组件,它可以将任意脚本作为动画的一部分运行,这允许在动画中播放所需的声音特效。

提示

除了play函数,还有许多类似于MediaPlayer提供的属性,例如,volumeloops。 后者可以设置为SoundEffect.Infinite以进行无限循环播放。 要停止播放,请调用stop函数。

警告

当使用PulseAudio后端时,调用stop不会立即停止,而只会阻止进一步的循环。 这是由于底层API限制的。

最后更新: 1/18/2022, 7:39:23 AM