user8024280
user8024280

Reputation:

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

Upvotes: 0

Views: 1888

Answers (1)

bipll
bipll

Reputation: 11940

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

Upvotes: 2

Related Questions