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

Audio files don't play in QML app on iPhone



  • Hi all,

    I installed one of my QML apps on an up to date iPhone. The project embraces a number of .wav files and are executed in the code this way:

    SoundEffect {
                id: winPlaySound
                source: "qrc:/Sounds/win.wav"
            }
    ...
    
    winPlaySound.play()
    ...
    

    The sounds are played on Desktop (Windows and Mac) and Android operating systems but when I ran the app on my iPhone, those sounds don't play. I also transferred the files directly to the iPhone and they work properly there.
    So why do they not play in the app, please?
    Is any settings modifying in the iPhone needed, please?


  • Moderators



  • @raven-worx
    Thanks so much for the answer. But the examples are huge in that sense we just merely want to play a simple audio file with a button. Also, they are beyond my QML knowledge, unfortunately.

    For instance, we have a simple audio file called win.mp3 or win.aac, and we want to play that file when we hit the green rectangle in the program on the iOS mobile. Please take a look at the QML program below:

    soundTest.pro:

    QT += quick
    CONFIG += c++11
    
    # The following define makes your compiler emit warnings if you use
    # any Qt feature that has been marked deprecated (the exact warnings
    # depend on your compiler). Refer to the documentation for the
    # deprecated API to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if it uses deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    SOURCES += \
            main.cpp
    
    RESOURCES += qml.qrc
    
    # Additional import path used to resolve QML modules in Qt Creator's code model
    QML_IMPORT_PATH =
    
    # Additional import path used to resolve QML modules just for Qt Quick Designer
    QML_DESIGNER_IMPORT_PATH =
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    

    main.cpp:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    

    main.qml:

    import QtQuick 2.12
    import QtQuick.Window 2.12
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        Rectangle {
                id: voice
                width: 50; height: 50
                color: "green"
                anchors.centerIn: parent
            }
    }
    

    What is the easiest way to modify this code to do the task of plying that audio file on the iDevice, please?


  • Moderators

    @tomy
    add this to your pro file

    ios{
        APP_Sounds.files = $$PWD/sounds/explosion01.mp3 \
                             $$PWD/sounds/explosion02.mp3
    
    
        APP_Sounds.files = sounds
        QMAKE_BUNDLE_DATA += APP_Sounds
    }
    

    inside your qml file

    AudioEngine {
            id:audioengine
    
            AudioSample {
                name:"explosion01"
                source: "sounds/explosion01.mp3"
            }
    
           AudioSample {
                name:"explosion02"
                source: "sounds/explosion02.mp3"
            }
    
            Sound {
                name:"explosion"
                playType: Sound.Random
                PlayVariation {
                    sample:"explosion01"
                    minPitch: 0.8
                    maxPitch: 1.1
                }
                PlayVariation {
                    sample:"explosion02"
                    minGain: 1.1
                    maxGain: 1.5
                }
            }
    
     MouseArea {
            anchors.fill: parent
            onPressed: {
                audioengine.sounds["explosion"].play();
            }
        }
    }
    

    QML is untested adaptation of
    https://doc.qt.io/qt-5/qml-qtaudioengine-sound.html



  • @J.Hilk

    Thanks so much.
    I added a test.mp3 file to the project as below, just to check the work out:

    0_1555082786034_1.png

    And added the lines below to the .pro file:

    ios {
        APP_Sounds.files = $$PWD/sounds/test.mp3
    
        APP_Sounds.files = sounds
        QMAKE_BUNDLE_DATA += APP_Sounds
    }
    

    And here is the Application Output window errors, when ran by the Deskto (Clang) kit:

    QML debugging is enabled. Only use this in a safe environment. default openal device = Built-in Output device list: Built-in Output add QDeclarativeAudioSample[ "test" ] add QDeclarativeSound[ "explosion" ] SoundCone: engine not changeable after initialization. Unknown child type for AudioEngine! AudioEngine begin initialization creating default category init samples 1 creating new StaticSoundBufferOpenAL init sounds 1 AudioEngine ready. QDeclarativeAudioEngine::dtor active = 0 pool = 0 for pool QAudioEngine::dtor QAudioEnginePrivate::dtor QAudioEnginePrivate::dtor: all done


  • Moderators

    @tomy well
    in your pro file the iostag means this is only executed if your target is the iOS platform.

    When you compile it for MacOS, which you said you tried, than you need

    macx {
    ....
    }
    

    just to make sure on the point

    $$PWD/sounds/test.mp3

    asumes, that your test.mp3 file is inside the sounds folder, and the sound folder is on the same hierarchy level as your pro file.

    otherwise the files won't be found and therefore won't be put into your app bundle



  • @J.Hilk
    Thank you.
    Well, the sounds folder, as it's shown on the screenshot above, is on a lower hierarchy level than the .pro file.
    Shouldn't I add the audio file on the qml.qrc file (which makes the sounds folder under it)? If not, so on what file/directory of Project's menu should I add the file, please?


  • Moderators

    @tomy
    the links @raven-worx posted post state that you can't use its resource system (qrc-files) for that. So you have to pack them with the app bundle itself.

    your folder should look like this:
    0_1555089283158_79d87e40-98dd-458b-afb4-4ed7d49225d5-image.png



  • @J.Hilk
    That folder, sounds, beforehand appeared that way. But this time I removed it from the qml.qrc file. Still the error messages exist, unfortunately. The screenshot below includes all windows and results:

    0_1555100328240_Screen Shot 2019-04-12 at 1.16.25 PM.png


  • Moderators

    @tomy
    ok, a couple of errors on my side. One should test before post actual code.

    the path in the profile should be a bit different to be relative to the executable.

    macx {
        APP_Sounds.files = $$PWD/sounds/Explosion_1.MP3 
    
        APP_Sounds.path = Contents/MacOS/sounds
    
        QMAKE_BUNDLE_DATA += APP_Sounds
    }
    

    you also need to add file:/// to tell Qt to use actual local files and not to search in the resource system.

    3rd I was unable to load a mp3 file from out of the appBundle, no matter what I tried. Maybe I'm missing something.
    So I hacked my way around it, by copying to the AppDataLocation in c++ .....

    It works, but I would encourage you to look for other/better solutions. Maybe someone else has more experience here.

    //pro
    QT += quick multimedia
    CONFIG += c++11
    
    SOURCES += \
            main.cpp
    
    RESOURCES += qml.qrc
    
    macx {
        APP_Sounds.files = $$PWD/sounds/Explosion_1.MP3 
        APP_Sounds.path = Contents/MacOS/sounds
        QMAKE_BUNDLE_DATA += APP_Sounds
    }
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    
    
    //main
    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    
    #include <QStandardPaths>
    #include <QFile>
    #include <QDir>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
    
        QString AppDataLocaltion = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
    
        if(!QFile::exists(AppDataLocaltion)){
            QDir d; d.mkdir(AppDataLocaltion);
        }
    
        QFile f("/sounds/Explosion_1.mp3");
        f.copy(AppDataLocaltion + "/Explosion_1.mp3");
    
        engine.rootContext()->setContextProperty("AppDataLocaltion", AppDataLocaltion);
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    
    //main.qml
    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtMultimedia 5.12
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        Text {
            anchors.fill: parent
            text: qsTr("Click me for sound")
    
            MouseArea {
                anchors.fill: parent
                onClicked: playExplosion.play()
            }
        }
    
        Audio{
            id: playExplosion
            source: "file://"+ AppDataLocaltion + "/Explosion_1.mp3"
            onErrorStringChanged: console.log("errorString",errorString, source.toString())
            onError: console.log(error)
        }
    }
    


  • @J.Hilk

    Thanks so much. It worked.
    Didn't change the main.cpp whatsoever, but just replaced "... + AppDataLocaltion + ..." with the actual address to the file:
    "file:///Users/ ... /Explosion_1.mp3", in main.qml.
    It's simpler, now. :)

    Next, if we want to use the process on iOS, is substituting the macx { ... } in the .pro file for the following version all we need, please?

    ios {
        APP_Sounds.files = $$PWD/sounds/Explosion_1.mp3
    
        APP_Sounds.path = Contents/iOS/sounds
        QMAKE_BUNDLE_DATA += APP_Sounds
    }
    

  • Moderators

    @tomy

    Didn't change the main.cpp whatsoever, but just replaced "... + AppDataLocaltion + ..." with the actual address to the file:
    "file:///Users/ ... /Explosion_1.mp3", in main.qml.
    It's simpler, now. :)

    it's simpler yes, but my point was it to make it viable for all platforms

    QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) exists for all platforms, but on some you have to create the folder first ->

    if(!QFile::exists(AppDataLocaltion)){
            QDir d; d.mkdir(AppDataLocaltion);
        }
    

    With a fixed local file path it will only work for your particular pc.

    Next, if we want to use the process on iOS, is substituting the macx { ... } in the .pro file for the following version all we need

    I'm actually not entirely sure.
    You may have to test the case :)


    that said, you could with the copy to appdata just place the mp3 in your qt resource system und copy them from there to local data.
    A workaround.



  • @J.Hilk

    Before the prior post I tested your (for all platforms) version. It showed some errors. But if we want to make it work for Windows, Mac and Android, the method is very easy and viable.

    For Windows and Android we use the qrc file, as you know. For Mac I use this way and it works too:

    1- Leave both main.cpp and .pro files intact (without any changes).
    2- Add the audio file in a folder and put it inside the project's directory.
    3- Add the line below to main.qml:

    Audio {
            id: playExplosion
            source: "file:///Users/ ... /soundTest/sounds/Explosion_1.mp3"
        }
    

    Up to now, we have covered three different platforms. Then we go for iOS.
    I made three different attempts as below:

    1- At the first attempt, I made no changes and just changed the kit to iOS on Qt Creator and ran the project. Result: Qt Creator installed the app on the iPhone successfully. "But" when pressing the rectangle, the sound won't play!

    2- At the second attempt I went for the .pro file and made only this change:
    QT += quick to QT += quick multimedia. Result: Just as the prior stage.

    3- At the third attempt, I made more changes to the .pro file, as follows:

    • QT += quick to QT += quick multimedia.
    • And adding this part at the end:
    ios {
        APP_Sounds.files = $$PWD/sounds/Explosion_1.mp3
    
        APP_Sounds.path = Contents/iOS/sounds
        QMAKE_BUNDLE_DATA += APP_Sounds
    }
    

    Result on iOS: two errors:
    unsealed contents present in the bundle root
    Xcodebuild failed.

    Result on the Mac: Works properly.

    Now, do you have any idea why it doesn't work on iOS, please?


  • Moderators

    @tomy
    so I checked. On iOS, there is no Content .... stuff

    In my last app I shipped the translations via app bundle:

    iOS {
        APP_Language.files = $$PWD/translations/myApp_de.qm \
                                                   $$PWD/translations/myApp_en.qm
    
        APP_Language.path = translations
        QMAKE_BUNDLE_DATA += APP_Languages
    }
    

    I than load it directly via:

    ...
    QString translationPath = QCoreApplication::applicationDirPath();
                   translationPath("/translations");
    
     m_translator->load("myApp_en.qm", translationPath);
    qApp->installTranslator(m_translator);
    

    works fine. If I adapt this to your situation:

    APP_Sounds.files = $$PWD/sounds/Explosion_1.MP3 
        APP_Sounds.path = sounds
        QMAKE_BUNDLE_DATA += APP_Sounds
    

    The file is shipped correctly and I can open it via QFile:

    QString soundPath = QCoreApplication::applicationDirPath() + QString("/sounds/Explosion_1.mp3");
    QFile f(soundPath);
    qDebug() << f.open(QIODevice::readOnly); // returns true
    f.close;
    
    QSound sound(soundPath);
    sound.play(); // returns QSoundEffect(qaudio): Error decoding source
    

    I can't get it to work,
    sry



  • @J.Hilk

    Modifying the ios{ ... } block in the .pro file removed the prior errors.
    I also added those lines into the code of main.cpp, and installed the project via Qt Creator on the phone, but no voice yet!
    That is, neither sound.play(); nor onClicked: playExplosion.play() plays the audio file on the phone. :(


Log in to reply