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...

  • What's the error? Can not find files?

  • [quote author="Chuck Gao" date="1310954575"]What's the error? Can not find files?[/quote]

    It's mute. If I use soundeffect it works, but then I can't change the volume.

  • I think it's a problem with path, there is no error(Output to the console) about this ?

  • [quote author="Chuck Gao" date="1311045100"]I think it's a problem with path, there is no error(Output to the console) about this ?[/quote]

    There's no error with path, since if I change it to SoundEffect it works..

  • I've a similar problem...
    On Symbian^3 and Harmattan if i embed the qml in the resource, sounds (using SoundEffect) don't hear!
    If i load qml as a local file, sounds work, but no images are loaded from the resource!!!

  • I have the same problem and it is not related to the path! It crashed the app.

  • Did anyone find a solution yet as I too have the same issue :(

  • I have solved this issue by using "file" protocol in audio source

    @Audio {
    id: playSound
    source: "file:///opt/YourProject/sounds/game.mp3"

  • I knew about file:///, but is there possibility to play sound from resource file?

  • Yes I have played sound from resource file but with some limitations e.g.

    • sound file size: 70kb approx.

    • Bit Rate: 8 bit

    • .wav files only

    • and the most worst thing is that It plays once.

  • 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 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. :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.