problem with QMediaPlayer
-
wrote on 26 Feb 2016, 10:40 last edited by
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?
-
Hi,
Do you mean that with
qrc:/
the sound is playing but you have no UI ? -
wrote on 28 Feb 2016, 15:34 last edited by valeriy
@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 changeqrc:/
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 :(
-
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.
-
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.
wrote on 28 Feb 2016, 21:56 last edited byI had the same problem.
It's because QMediaPlayer needs a QUrl and QUrl must start with something likeqrc:/
orhttp:/
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 QUrlQUrl 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.
-
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.
-
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.
wrote on 29 Feb 2016, 00:48 last edited by@SGaist I use Qt 5.4.1 on Ubuntu 14.04 64-bit
-
It's been added to Qt 5.5.0
1/8