[solved] Sounds on Android?



  • Hi,
    does somebody know why I'm getting this error when trying to play a sound on Android?

    W/Qt (16485): audio\qsoundeffect_qaudio_p.cpp:354 (void PrivateSoundSource::decoderError()): QSoundEffect(qaudio): Error decoding source

    I am using QSound like:

    @#include <QSound>

    ...

    QSound::play("../sounds/mysound.wav");@

    and mysound.wav-file is located as "../sounds/mysound.wav" states. And everything works perfectly on Windows. What could be wrong?

    Should I possibly configure .pro-file somehow to make sounds deploy with app?


  • Lifetime Qt Champion

    Hi,

    Are you really sure the file can be found on Android ?



  • No, I'm not sure. I don't know if Qt copies that file automatically on Android device or not.

    But if it doesn't, then what should I do to fix it?

    I would like to configure Qt to somehow copy that file for me, because in the future I need to make a complete .apk file, which should contain everything that my app needs for running correctly.


  • Lifetime Qt Champion



  • Thanks!

    With the keyword "assets", I found this link:
    http://qt-project.org/doc/qt-5/platform-notes-android.html

    So, I created assets-folder under myproject/android/, and moved sounds there, and now they DO work on my phone when I call
    @ QSound::play( "assets:/sounds/mysound.wav );@

    There is just one drawback - that broke sounds on my Windows environment.
    .
    .
    .
    Is there any elegant way to tell Qt to call
    @ QSound::play( "assets:/sounds/mysound.wav );@
    in Android, and
    @ QSound::play( "../my/path/sounds/mysound.wav " );@
    in Windows?
    .
    .
    .
    p.s. Even QSound::play("android/assets/sounds/mysound.wav "); won't work in Windows. The problem seems to be related to ./android-path, because sounds in other directories do work. Any ideas why?


  • Lifetime Qt Champion

    You can use

    @#ifdef Q_OS_ANDROID
    QString mySoundPath = "assets:/sounds/mysound.wav";
    #else
    QString mySoundPath = "../my/path/sounds/mysound.wav";
    #endif
    QSound::play(mySoundPath);
    @

    You are giving a relative path, so the folders containing your sound should be at the same level as you executable, which is not the case using a shadow build



  • Once again, your solution worked - thanks!

    But how I should reference to my sounds directory on Windows, then?

    Until now, I have used "../sounds/", which is working, because my sounds are one directory upper than my code files. But when I build the app, the .exe will go two directories lower than sounds directory. And it still works.

    I don't understand?


  • Lifetime Qt Champion

    Good question, maybe you have a copy at the right place ? Or the working directory is modified somehow ?



  • [quote author="evocat" date="1407009118"]Once again, your solution worked - thanks! But how I should reference to my sounds directory on Windows, then? Until now, I have used "../sounds/", which is working, because my sounds are one directory upper than my code files. But when I build the app, the .exe will go two directories lower than sounds directory. And it still works. I don't understand? [/quote]

    Why wouldn't you use Qt resource file then? It perfectly works with linux and android without any source code changes. I believe windows is not a problem too.


  • Lifetime Qt Champion

    Sure it works on all platform. What must be taken in account now is the size of the audio files. wav being uncompressed it can quickly be too big to be used in a builtin resource file.



  • Good work!!! I have I had the same problem.

    To resume the steps are the following:

    1. add multimedia in .pro file

    @QT += core gui multimedia@

    1. add QSound in the mainwindow.h

    @#include <QSound>@

    1. add your sound file under the directory android\sounds (not mandatory)

    android\sounds\mysound.wav

    1. add the mysound.wav as resource then create a resource.qrc file and put inside the file mysound.wav

    2. in your code load the file in this way:

    @sound = new QSound(":/sounds/done.wav");@

    1. play the file in this way:

    @sound->play();@

    There are several methods as stop(), loop() etc.....


Log in to reply
 

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