PDA

View Full Version : How to play a sound from a resource file?



Asperamanca
21st July 2010, 12:54
Hi,

I would like to play a wave file, and it would make sense to include that file into my resource files (although I'm ready to accept a different suggestion).

I have been able to play the wave file using QSound and an absolute path. All my attempts to use a relative path, or a resource path, were in vain.

I have verified that my resource paths work in general by displaying a pixmap using ":/images/myPixmap.png". I have added my wave file in a similar way (<file>sounds/clock.wav</file> )

By now, I have learned that QSound does not support the resource path system (with ":/"). Okay...so how do I translate ":/sounds/clock.wav" to a filename that QSound can use?

I've tried using QDir::absoluteFilePath, but it simply appends, and does not resolve the resource path. I've tried QFile as well as QResource, but the fileName or absoluteFileName returned is again simply what I fed it in the constructor. I've read the hint to set a searchPath in QDir, but for that, I need to find out my resource path first.

How do I either
a) resolve a resource file name like ":/sounds/mySound.wav" to the absolute file name, or
b) Find out my resource base path, so I can use relative path access?

Many thanks!

saa7_go
21st July 2010, 17:13
What if you use QTemporaryFile (http://doc.trolltech.com/4.6/qtemporaryfile.html)?

Asperamanca
21st July 2010, 17:26
You mean, creating a temporary file on disc from the binary information I get from QResource? That would be a bit overkill for my simple needs.

It seems I had misunderstood the resource system when I first posted. I've since found a solution without using it - QDir after all (I just misunderstood the starting path of my application, so the sound file was not where the application expected it).

Anyway, thanks!

Lykurg
21st July 2010, 17:28
Have a look at our wiki: Current working directory. It might help you.