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

  • The impact of a data loss event can be significant. Real-time data is essential to remaining competitive. Many companies can no longer afford to rely on a truck arriving each day to take backup tapes offsite. For most companies, a cloud backup and recovery solution will eliminate, or significantly reduce, IT resources related to the mundane task of backup and allow your resources to be redeployed to more strategic projects. The cloud - can now be comfortable for you – with 100% recovery from anywhere all …

  • Hurricane Sandy was one of the most destructive natural disasters that the United States has ever experienced. Read this success story to learn how Datto protected its partners and their customers with proactive business continuity planning, heroic employee efforts, and the right mix of technology and support. With storm surges over 12 feet, winds that exceeded 90 mph, and a diameter spanning more than 900 miles, Sandy resulted in power outages to approximately 7.5 million people, and caused an estimated $50 …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds