Solved MediaPlayer doesn't work as expected
-
This project is created on Qt 5.12.3:
Here's the
main.qml
:import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.5 import QtMultimedia 5.12 Window { id: window width: 800; height: 600 visible: true color: "gray" Image { source: "qrc:/images/background.png" width: window.width / 1.2 height: window.height / 1.2 anchors.centerIn: parent } Component.onCompleted: startGameSound.play() MediaPlayer { id: startGameSound source: "qrc:/sounds/startGame.mp3" volume: 0.2 } }
I put the file startGame.mp3 in the sounds folder inside the project's directory and then added that on the qrc file.
Now when run by the Desktop kit, the image background.png is shown but that audio file won't play!
It was working very well on Qt 5.12.1, but I don't know why it doesn't using that new version of Qt.
I get this message in the Application Output window:defaultServiceProvider::requestService(): no service found for - "org.qt-project.qt.mediaplayer"
-
One thing I found:
Since I'm using a 64-bit version of Windows, so I changed the Desktop kit from MinGW 32-bit to MinGW 64-bit, and it worked! :)
And that "MinGW 64-bit" is new in Qt 5.12.3 and I used to use "MinGW 32-bit" as before, so faced that strange behavior! When changing to "MinGW 64-bit" the problem is worked out!
Apparently on a 64-bit version of the machine we need to use the 64-bit version of Qt which is newly provided.
-
@tomy It is perhaps a stupid question but: do you have add multimedia support in you pro file?
QT += multimedia
-
@KroMignon
Hi,
I think that is not needed.
I remember that "multimedia" was needed only on Mac and we needn't use it for Windows in Qt 5.12.1, but Qt 5.12.3 acts strangely!The problem whatever it is, it is related to MediPlayer.
For example when I replace that MediPlayer part with:
SoundEffect { id: winGameSound source: "qrc:/sounds/winGame.wav" }
It works without any message.
-
@tomy what is your target platform: Linux, Windows, MacOS??
-
I'm testing this program on Windows 10 x64.
-
@tomy Don't think it will change anything, but just to check it. Have you tried to set autoPlay?
import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.5 import QtMultimedia 5.12 Window { id: window width: 800; height: 600 visible: true color: "gray" Image { source: "qrc:/images/background.png" width: window.width / 1.2 height: window.height / 1.2 anchors.centerIn: parent } MediaPlayer { id: startGameSound source: "qrc:/sounds/startGame.mp3" volume: 0.2 autoPlay: true } }
-
@tomy you can also check if there is an error case.
import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.5 import QtMultimedia 5.12 Window { id: window width: 800; height: 600 visible: true color: "gray" Image { source: "qrc:/images/background.png" width: window.width / 1.2 height: window.height / 1.2 anchors.centerIn: parent } MediaPlayer { id: startGameSound source: "qrc:/sounds/startGame.mp3" volume: 0.2 autoPlay: true onError: console.log("MediaPlayer error: " + errorString) } }
-
I get this error:
qml: MediaPlayer error: The QMediaPlayer object does not have a valid service
-
One thing I found:
Since I'm using a 64-bit version of Windows, so I changed the Desktop kit from MinGW 32-bit to MinGW 64-bit, and it worked! :)
And that "MinGW 64-bit" is new in Qt 5.12.3 and I used to use "MinGW 32-bit" as before, so faced that strange behavior! When changing to "MinGW 64-bit" the problem is worked out!
Apparently on a 64-bit version of the machine we need to use the 64-bit version of Qt which is newly provided.