Nominate our 2022 Qt Champions!

problem with QMediaPlayer

  • i created resourse file and added mp3 file
    write 3 lines:

    QMediaPlayer* player = new QMediaPlayer();

    application is started, there is win process, but does not appear on the screen
    later, i changed code:

    QMediaPlayer* player = new QMediaPlayer();

    application is run on the screen, but now sound not play

    my .pro file

    QT += core gui multimedia
    TARGET = test_cpp
    CONFIG += console c++11
    CONFIG -= app_bundle
    TEMPLATE = app
    SOURCES += main.cpp \
        Figure.cpp \
    HEADERS += \
        Figure.h \
    RESOURCES += \

    what am I doing wrong?

  • Lifetime Qt Champion


    Do you mean that with qrc:/ the sound is playing but you have no UI ?

  • @SGaist said:


    Do you mean that with qrc:/ the sound is playing but you have no UI ?

    sound isn't playing and the application does not appear on the screen.
    If i change qrc:/ to :/, sound isn`t playing, but application is running normally.
    I noticed that happens only in this class:

    class Button : public QObject, public QGraphicsPixmapItem
        QPixmap *state1, *state2;
        void mousePressEvent(QGraphicsSceneMouseEvent *event);
        void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
        void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
        //QMediaPlayer *hoverSound;
        Button(const QPixmap& pixmap1, const QPixmap& pixmap2);
        void clicked();

    ps. sorry for my english :(

  • Lifetime Qt Champion

    Depending on your target platform reading from qrc is not possible. That's not a Qt limitation but a platform constraint. The native API used are not able to access data from Qt's resource. You should copy the file to a temporary folder provided by QStandardPaths and play it from there.

  • I had the same problem.
    It's because QMediaPlayer needs a QUrl and QUrl must start with something like qrc:/ or http:/ etc. otherwise string is an invalid QUrl and QUrl becomes empty.

    QUrl url1("qrc:/path to your file in resources");
    QUrl url2(":/path to your file in resources");
    qDebug() << url1;
    qDebug() << url2;

    url2 is empty. because it's not a valid url.
    I don't know if it is possible to disable validation in QUrl or not.
    I add a "qrc" manually to my string when passing to QUrl

    QUrl url("qrc" + ":/path to file in resources");

    @SGaist why there is no access to library or binary file in some platforms? resources are internal and are in application binary.

  • Lifetime Qt Champion

    Your url1 should be correct.

    What Version of Qt are you using ? On what platform ?

    I've doubled checked the implementation of QMediaPlayer, if you have a recent enough Qt, it should do the temporary file trick automatically for you.

    It's because the native API don't know how to handle files in Qt's resources.

  • @SGaist I use Qt 5.4.1 on Ubuntu 14.04 64-bit

  • Lifetime Qt Champion

    It's been added to Qt 5.5.0

Log in to reply