[solved] Sounds on Android?
-
wrote on 29 Jul 2014, 19:53 last edited by
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?
-
Hi,
Are you really sure the file can be found on Android ?
-
wrote on 30 Jul 2014, 08:03 last edited by
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.
-
"this":https://community.kde.org/Necessitas/Assets might help
-
wrote on 2 Aug 2014, 19:10 last edited by
Thanks!
With the keyword "assets", I found this link:
http://qt-project.org/doc/qt-5/platform-notes-android.htmlSo, 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? -
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
-
wrote on 2 Aug 2014, 19:51 last edited by
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?
-
Good question, maybe you have a copy at the right place ? Or the working directory is modified somehow ?
-
wrote on 5 Aug 2014, 06:06 last edited by
[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.
-
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.
-
wrote on 27 Aug 2014, 22:34 last edited by
Good work!!! I have I had the same problem.
To resume the steps are the following:
- add multimedia in .pro file
@QT += core gui multimedia@
- add QSound in the mainwindow.h
@#include <QSound>@
- add your sound file under the directory android\sounds (not mandatory)
android\sounds\mysound.wav
-
add the mysound.wav as resource then create a resource.qrc file and put inside the file mysound.wav
-
in your code load the file in this way:
@sound = new QSound(":/sounds/done.wav");@
- play the file in this way:
@sound->play();@
There are several methods as stop(), loop() etc.....