Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to reference bundled files on ios?



  • Hey, from here and hereI got the way to make the files bundled in the project so I can see them in "Bundle data" folder (without the audio folder) in Xcode environment. How do I correctly reference them in qml?

    In my pro file I have this:

    ios: {
        BUNDLE_DATA.files = $$files($$PWD/audio/*.mp3)
        QMAKE_BUNDLE_DATA += BUNDLE_DATA
    }
    

    Seems to work...

    But in my qml I have tried all of these but none seems to be working:

    qrc:/audio/file.mp3  //this is the default one that works on other platforms without the need to bundle anything
    file:/audio/file.mp3
    audio/file.mp3
    file.mp3
    

    Thanks and have a great day!


  • Lifetime Qt Champion

    Hi,

    Based on your first link, your forgot the

    BUNDLE_DATA.path = Documents
    

    line.



  • @SGaist Thank you for your reply! I added the Documents path (also forgot to include the plugin so did that too) but now when leaving the url like

    qrc:/audio/file.mp3
    

    I am getting Failed to load media error

    I tried to change the url in the same way as in the first link to simply:

    file.mp3
    or
    qrc:/file.mp3
    

    and then I am getting Attempting to play invalid QT resource

    I believe that it simply cannot be found. In Xcode I see the file in Bundle data folder without the original audio subfolder. So how to tell qml that we are referring to that location? Maybe the first one actually finds the file but doesn't load it for some reason?

    EDIT: I also found possibly relevant answer here but I am not sure if the bundle data is behaving as normal qrc resource or not or how to locate it properly.


  • Lifetime Qt Champion

    qrc:// is for Qt's resource system which are built in your executable.

    Can you check the content of your app bundle ? It should give you an idea of where the file is located.



  • @SGaist Thank you! I have inspected the package according to this. When I open the package I see the Documents folder (probably the one I defined in the path variable) where the mp3 file is (so package/Documents/file.mp3).
    So I have tried:

    file:/Documents/file.mp3 - Failed to load media (this message is shown even if I write bad path on purpose)
    Documents/file.mp3 - Attempting to play invalid QT resource
    

    This is really strange. The guy in the first link even omitted the "Documents". I have no idea how he made it work. The path should be relative to the root of the package right? One idea I got is, should I use C++ helper function to somehow get absolute path on the device? Not sure how would I reference the file in C++ either.


  • Lifetime Qt Champion

    Can you show what the folder tree looks like exactly ?

    One thing you can try is to go from the exec folder and move up enough to accent Documents.



  • @SGaist When I open the package file in the simulator folder I see this (you can see also how the mp3 file is present in Xcode).
    I got this idea to use my file picker that I have in the app using FolderListModel. I tried to set the start dir to "./" and I ended up in qrc:/ system seeing the qml modules. When I set it to "file:/./" I end up in some folder with Application, Library, System and Users subfolders. The conclusion is that whenever I use simply a path string in points to path relative to qrc cache.
    I got the idea to use StandardPaths.AppDataLocation and yea I can see the Documents folder there! So tried to use the same in the MediaPlayer:

    StandardPaths.locate(StandardPaths.AppDataLocation, "file.mp3", StandardPaths.LocateFile)
    

    And it actually finds the file! The only problem is that i had to remove the "path" variable from pro file since the locate function probably doesn't work recursively. Without the path the files are places in the root folder of the package. When I print the url I see an absolute path leading to the mp3 file.
    One last problem, the MediaPlayer is still not working...Failed to load media.... :-/
    But maybe this is a different problem. I am creating the ios builds on virtual machine where sound is not working for some reason and now I found out that I cannot play any mp3 files on that machine. I might try this on some real devices now cause I think that the file is now properly found and passed to the MediaPlayer.
    What do you think?


  • Lifetime Qt Champion

    I'd change as well. If no audio hardware is detected, I would think it's normal that the media player fails.



  • @SGaist OK thank you so much! I'll mark the thread as resolved if I make it work on other devices.



  • @SGaist Hey so the reference problem is solved. Seems like the file can be opened, the player works but there is no sound :-/. I'll probably open another thread for this issue. Thanks for the help again!
    EDIT: Or maybe it's working :D. On other devices I got a report that the sound is OK. Will investigate that further!