qml_sound
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
import QtQuick 2.9
import QtQuick.Window 2.2
import QtMultimedia 5.0
Window
{
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Text
{
id: button
text: qsTr("Play");
anchors.centerIn: parent
}
Audio
{
id: music
source: "example.ogg"
}
MouseArea
{
anchors.fill: parent
onPressed:
{
if (music.playbackState == Audio.StoppedState)
{
music.play();
button.text = qsTr("Stop");
}
else
{
music.stop();
button.text = qsTr("Play");
}
}
}
}