Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. problem with QMediaPlayer
QtWS25 Last Chance

problem with QMediaPlayer

Scheduled Pinned Locked Moved Unsolved Installation and Deployment
8 Posts 3 Posters 2.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    valeriy
    wrote on last edited by
    #1

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

    QMediaPlayer* player = new QMediaPlayer();
    player->setMedia(QUrl("qrc:/sounds/psk.mp3"));
    player->play();
    

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

    QMediaPlayer* player = new QMediaPlayer();
    player->setMedia(QUrl(":/sounds/psk.mp3"));
    player->play();
    

    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 \
        GameField.cpp
    
    HEADERS += \
        Figure.h \
        GameField.h
    
    RESOURCES += \
        res.qrc
    

    what am I doing wrong?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      V 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

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

        V Offline
        V Offline
        valeriy
        wrote on last edited by valeriy
        #3

        @SGaist said:

        Hi,

        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
        {
            Q_OBJECT
        private:
            QPixmap *state1, *state2;
            void mousePressEvent(QGraphicsSceneMouseEvent *event);
            void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
            void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
         
            //QMediaPlayer *hoverSound;
         
        public:
            Button(const QPixmap& pixmap1, const QPixmap& pixmap2);
            ~Button();
         
        signals:
            void clicked();
        };
        

        ps. sorry for my english :(

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply
          0
          • SGaistS SGaist

            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.

            M Offline
            M Offline
            MajidKamali
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              M 1 Reply Last reply
              0
              • SGaistS SGaist

                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.

                M Offline
                M Offline
                MajidKamali
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  It's been added to Qt 5.5.0

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved