Audio element not working? How to play from resource?
-
I'm having problems using audio element from qml file located in resource file.
If my qml file is not in resource, the sound plays just fine. But instantly after I put the qml file to the resource, I can't hear the audio element anymore. What should I do? I have tried putting the sound file to resource and to use it from file path...
-
You can leave you sound file outside of resource file, then use
@viewer.rootContext()->setContextProperty("installPath", "file:///" + QCoreApplication::applicationDirPath() + "/");@
in your main.cpp, make sure this line is before you setsource("main.qml")
In you qml code, just use
@ Audio {
id: playMusicAudio
volume: 1.0
source: installPath + "sound/youmusic.mp3"
}@You need to have this in you app.pro file
@# Add more folders to ship with the application, here
folder_01.source = qml/yourappname/sound
DEPLOYMENTFOLDERS = folder_01@ -
Perhaps it would be sensible to write a C++ wrapper that would read the mp3 audio file from the resource and feed it to QSound somehow? Is this even an issue anymore? Just stumbled upon this thing today when learning QML - noticed that my Audio {} would not play an .mp3 from within a resource and I'd prefer not to ship separate audio files.
I'd bet there already exists a robust solution, will keep googling.
edit: Guess I'll try writing a C++ component that wraps the Phonon player. Somehow strange that images work fine from within qrc but not media..?
-
Doesnt seem to be happening with QMediaPlayer API either.. keep getting QMediaPlayer::FormatError , "Failed to load media" for the qrc sources.
Probably could write the resource file into /tmp file and play from there but that would be quite silly. Guess I'll ship the audio files with the app as separate files. :)