简体   繁体   English

显示qml时如何在QML中播放声音

[英]How to play sound in QML when qml is shown

I need to play a sound in QML using SoundEffect, but all examples which I found play the sound based on some event (mouse clicked and so on ), but how to play the sound when the qml is shown? 我需要使用SoundEffect在QML中播放声音,但是我发现的所有示例都是基于某个事件(鼠标单击等等)播放声音的,但是显示qml时如何播放声音?

This is example with mouse clicked: 这是鼠标单击的示例:

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

You probably need a signal Component.completed that is emitted whenever an object that implements a component is instantiated: 您可能需要实例化实现组件的对象时发出的Component.completed信号:

MouseArea {
    id: playArea
    Component.onCompleted: playSound.play()
}

If it really is a graphical item and you need a sound played each time the item becomes visible, then handle the visibleChanged signal, for instance: 如果它确实是一个图形项目,并且每次该项目变为可见状态时都需要播放声音,则处理visibleChanged信号,例如:

Rectangle {
    id: soundBox
    onVisibleChanged: if(visible) playSound.play()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM