Results 1 to 4 of 4

Thread: How to play a sound from a resource file?

  1. #1
    Join Date
    Jul 2009
    Posts
    74
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question How to play a sound from a resource file?

    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!

  2. #2
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: How to play a sound from a resource file?

    What if you use QTemporaryFile?
    Last edited by saa7_go; 21st July 2010 at 17:16. Reason: updated contents

  3. #3
    Join Date
    Jul 2009
    Posts
    74
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to play a sound from a resource file?

    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!

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to play a sound from a resource file?

    Have a look at our wiki: [WIKI]Current working directory[/WIKI]. It might help you.

Similar Threads

  1. Play .amr sound
    By ahmdsd_ostora in forum Qt Programming
    Replies: 3
    Last Post: 17th July 2010, 15:34
  2. Replies: 6
    Last Post: 23rd December 2009, 10:45
  3. Phonon Cannot Play Sound (without Error)
    By padawan in forum Qt Programming
    Replies: 5
    Last Post: 16th September 2009, 15:17
  4. S60, garden and play sound with native functions
    By Lykurg in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 29th May 2009, 19:52
  5. MP3 Sound / OGG Sound Play on GUI
    By patrik08 in forum Newbie
    Replies: 3
    Last Post: 1st September 2006, 19:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.