简体   繁体   中英

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?

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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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