C++ Builder

La Isla Bonita



PlaySound (Win32 API)
PlaySound (Win32 API)


Description

This example shows a wrapper that uses the Win32 API PlaySound() function . The wrapper uses one less parameter than the API.
The FileName should be expressed, for example as: "C:\\_1MM\\Jungle Critical Stop.wav"

Form1 has a TButton.

Files

PlaySound.cpp

#include <vcl.h>
#include <mmsystem.h>    //For PlaySound()
#pragma hdrstop

#include "PlaySound.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

bool Play_Sound(LPCSTR FileName, DWORD PlayMode)
{
    switch(PlayMode)
    {
        case SND_APPLICATION:
             // The sound is played using an the sound associated with
             // the application.
             return PlaySound(FileName, NULL, SND_APPLICATION);

        case SND_SYNC:
             // Return after finishing playing the sound.
             return PlaySound(FileName, NULL, SND_SYNC);

        case SND_ASYNC:
             // Return immediately after beginning playing the sound.
             return PlaySound(FileName, NULL, SND_ASYNC);

        case SND_LOOP:
             // Play sound repeatedly.
             return PlaySound(FileName, NULL, SND_LOOP | SND_ASYNC);

        case SND_NODEFAULT:
             // If the sound cannot be found, PlaySound returns
             // without playing the default sound.
             return PlaySound(FileName, NULL, SND_NODEFAULT);

       default: // Error: Illegal Play Mode
             return false;
    }
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    if (!Play_Sound("C:\\_1MM\\Jungle Critical Stop.wav", SND_NODEFAULT))
    {
        ShowMessage("Play_Sound: PlaySound API failed");
    }
}
//---------------------------------------------------------------------------

PlaySound.h

#ifndef PlaySoundH
#define PlaySoundH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        TButton *Button1;
        void __fastcall Button1Click(TObject *Sender);
private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Homepage

Copyright © 1997-2003 Rodolfo A. Frino. All rights reserved.