/* .pro TARGET = recon_qsoundeffect QT += multimedia widgets HEADERS = $${TARGET}.h */ constexpr char PATH_TO_SOME_LOCAL_WAV[] = "some.wav"; #include #include #include #include #include #include #include class MyWindow : public QMainWindow { Q_OBJECT public: MyWindow(); virtual ~MyWindow() override {} private: QSoundEffect m_sound; }; MyWindow::MyWindow() { setWindowTitle(QString("Qt") + QT_VERSION_STR); auto widget = new QWidget(this); auto verticalLayout = new QVBoxLayout(); widget->setLayout(verticalLayout); setCentralWidget(widget); if (!QFileInfo::exists(PATH_TO_SOME_LOCAL_WAV)) { verticalLayout->addWidget(new QLabel(QString("Please put the '%1' file in the working directory!").arg(PATH_TO_SOME_LOCAL_WAV))); } else { widget->setFixedWidth(300); QPushButton* btnSource = new QPushButton(widget); btnSource->setText("set source"); verticalLayout->addWidget(btnSource); QPushButton* btnPlay = new QPushButton(widget); btnPlay->setText("play"); verticalLayout->addWidget(btnPlay); btnPlay->setEnabled(false); connect(&m_sound, &QSoundEffect::statusChanged, [this, btnPlay] { btnPlay->setEnabled(m_sound.isLoaded()); }); connect(btnSource, &QPushButton::clicked, [this] { m_sound.setSource(QUrl::fromLocalFile(PATH_TO_SOME_LOCAL_WAV)); }); connect(btnPlay, &QPushButton::clicked, [this] { m_sound.play(); }); #if QT_VERSION >= QT_VERSION_CHECK(6,0,0) if (QSoundEffect::supportedMimeTypes().size() < 1) btnSource->setText(btnSource->text() + "\n(will crash the app)"); #endif } } int main(int argc, char *argv[]) { QApplication app(argc, argv); MyWindow window; window.show(); return app.exec(); }