QMediaPlayer doesn't produce audio

吃可爱长大的小学妹 提交于 2019-12-02 10:22:29

Your application is missing a

  • QCoreApplication if it is supposed to be headless
  • QGuiApplication for QtQuick, or
  • QApplication if it features Widgets

Q*Application is a mandatory component for most Qt applications, as this is the piece that processes all events and signal on the main thread. This is the reason why you are having QTimer related errors, as Qt was not able to "wrap" the main thread with a QThread beforehand.

Just add it, as well as app.exec(); to start it, and you should be fine. app.exec() will block until your application finishes.

Also, instances that you need during the whole lifetime of the application should usually be created on the stack, instead of the heap.

int main(int argc, char *argv[]) {
    QCoreApplication app(argc, argv);

    QMediaPlaylist list;
    auto media = QUrl::fromLocalFile(QFileInfo("Filename.mp3").absoluteFilePath());
    list.addMedia(media);

    QMediaPlayer music;
    music.setPlaylist(list);
    music.play();

    return app.exec();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!