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

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 23, 2014 @ 12:00 p.m. ET / 9:00 a.m. PT Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this eSeminar to learn why physical appliances are preferable to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds