Book Image

Application Development with Qt Creator - Second Edition

Book Image

Application Development with Qt Creator - Second Edition

Overview of this book

Table of Contents (20 chapters)
Application Development with Qt Creator Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Playing audio clips and sound effects


Qt Quick provides the SoundEffect type to play short sound effects with a minimum latency. This is especially good for things such as button click sounds, virtual keyboard sounds, and alert tones as part of a rich and engaging multimedia experience. Using it is straightforward; you provide the type with a source field and call its play method to start the playback, as follows:

import QtQuick 2.3
import QtMultimedia 5.0

Text {
    text: "Click Me!";
    font.pointSize: 24;
    width: 150; height: 50;

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

Here, SoundEffect will play the contents of the soundeffect.wav file stored in the application's resource when you click on the mouse area. You can stop a sound effect by calling its stop method, or else it simply plays until it gets completed.

The SoundEffect type...