Play Wav resource

Well, this doesn't really deal with MFC, but its useful knowledge if you don't know how..

Parts of this code I ripped from VC++ 4.1 help, but it was outdated and didn't come close to working.

First of all you need to add the wave files to the .rc file manually like so:

<NameOfSound> WAVE <Location of WAVE.>
Example being
Cool WAVE C:\projects\sounds\cool.wav

Then you need to add this function declarion to the class you plan on using..
BOOL PlayResource(LPSTR lpName)
{
    BOOL bRtn;
    LPSTR lpRes;
    HANDLE hRes;
    HRSRC hResInfo;
    HINSTANCE Nl=AfxGetInstanceHandle();

    /* Find the WAVE resource. */
    hResInfo= FindResource(Nl,lpName,"WAVE");
    if(hResInfo == NULL)
       return FALSE;
    /* Load the WAVE resource. */

    hRes = LoadResource(Nl,hResInfo);
    if (hRes == NULL)
      return FALSE;

    /* Lock the WAVE resource and play it. */
    lpRes=(LPSTR)LockResource(hRes);
    if(lpRes==NULL)
      return FALSE;

    bRtn = sndPlaySound(lpRes, SND_MEMORY | SND_SYNC);
    if(bRtn == NULL)
      return FALSE;

    /* Free the WAVE resource and return success or failure. */
    FreeResource(hRes);
    return TRUE;
}

Then to play the sound you simply use:

PlayResource("<soundname>");
Example being
PlayResource("Cool");



Comments

  • How to play *.wav when we store it on database

    Posted by Legacy on 01/20/2004 12:00am

    Originally posted by: romi nur ismanto

    How to play *.wav when we store it on database
    Please give me code to play *.wav when thats file is in the database (acces)

    Reply
  • How can I pause a long wav file and then resume it?

    Posted by Legacy on 10/22/2003 12:00am

    Originally posted by: Wenjun

    I am using PlaySound() to play wav files. However, I did not find the command to pause and resume the playing. Your kind help is highly appreciated.

    Reply
  • Audio Player Drag and Drop

    Posted by Legacy on 05/13/2003 12:00am

    Originally posted by: Tapas

    Hi i am making a audio player. i have problem abt this.
    
    

    1. If i click on wav file it opens the apllication but can't play it.
    2. if i open a file from file open menu it comes to play list but can't play.

    pls give any suggestion.

    tapas

    Reply
  • Resource putting automation

    Posted by Legacy on 05/07/2003 12:00am

    Originally posted by: CallMan

    Tell me please - how can i write into the resource file a prety big
    amount of WAV's ? I do not want to make it manually (Menu -> Insert
    ->Resource, etc. )

    Can i in some way automatise this job ?

    Reply
  • How to play wav on internet ..... Data Streaming

    Posted by Legacy on 12/02/2002 12:00am

    Originally posted by: Maggie

    How to perform Data Streaming on the web page????

    • try this to play a wav file

      Posted by ellav on 03/31/2006 09:17am

      Personally liked the way of Waveform Audio Interface: in just 2 lines you can play a file in PCM as wav file in c# for compact framework 1.0 VS .NET 2003: //Play a filname.wav WaveOut wo = new WaveOut(); wo.Play(filename, 512*1024, 0xffff, 0xffff); //Thats all thanks o the P/Invoke Library Sample available a MSDN help

      Reply
    Reply
  • How do you put a background sound in a form in Visual Basic 6

    Posted by Legacy on 11/25/2002 12:00am

    Originally posted by: Thomas Hair

    How do you put a background sound in a form in Visual Basic 6. I really need to know this for a project that I am making. The audio file is a MP3 file 9is this possible to use as a background sound or does it have to be a WAVE file?)

    Reply
  • How tp play from buffer?

    Posted by Legacy on 08/06/2002 12:00am

    Originally posted by: Anurag

    How would you play the sound streaming in to a buffer? (not a file.. just a buffer..)

    Reply
  • Wave Streams

    Posted by Legacy on 07/28/2002 12:00am

    Originally posted by: gsc

    Are wave streams for playing multiple wave files simultaneosly? If not, how is this done. I believe it is possible to do this using wave channels, or something, but I am have no idea how. If anyone knows, please help me out by explaining this in your reply. Thanks.

    Reply
  • Do it the easy way

    Posted by Legacy on 07/16/2002 12:00am

    Originally posted by: sanpee

    Would it be easier if you just say
    PlaySound(MAKEINTRESOURCE(ID...),hModule,SND_RESOURCE|SND_ASYNC);

    Reply
  • How to get the total time of a wav file?

    Posted by Legacy on 06/24/2002 12:00am

    Originally posted by: jokerhe

    Thanks.

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds