Annunci

Linkedin


Riprodurre un suono wav in maniera semplice

venerdì, dicembre 10, 2010 @ 10:12 AM admin

Per riprodurre un file audio di tipo wav utiliamo una API molto immediata inclusa nella unit MMSystem:

BOOL PlaySound(pszSound:PAnsiChar; hmod: Cardinal; fdwSound: Cardinal);


Descrizione dei parametri

pszSound : stringa che specifica il suono da riprodurre. Se diamo a questo parametro NULL tutte le riproduzioni wav si fermano.

hmod: Handle dell’eseguibile che contiene la risorsa da caricare.

fdwSound: flags che può assumere i seguenti valori

SND_APPLICATION: The sound is played using an application-specific association.

SND_ALIAS: The pszSound parameter is a system-event alias in the registry or the WIN.INI file. Do not use with either SND_FILENAME or SND_RESOURCE.

SND_ALIAS_ID:  The pszSound parameter is a predefined sound identifier.

SND_ASYNC: The sound is played asynchronously and PlaySound returns immediately after beginning the sound. To terminate an asynchronously played waveform sound, call PlaySound with pszSound set to NULL.

SND_FILENAME: The pszSound parameter is a filename.

SND_LOOP: The sound plays repeatedly until PlaySound is called again with the pszSound parameter set to NULL. You must also specify the SND_ASYNC flag to indicate an asynchronous sound event.

SND_MEMORY:  A sound event’s file is loaded in RAM. The parameter specified by pszSound must point to an image of a sound in memory.

SND_NODEFAULT: No default sound event is used. If the sound cannot be found, PlaySound returns silently without playing the default sound.

SND_NOSTOP: The specified sound event will yield to another sound event that is already playing. If a sound cannot be played because the resource needed to generate that sound is busy playing another sound, the function immediately returns FALSE without playing the requested sound.
If this flag is not specified, PlaySound attempts to stop the currently playing sound so that the device can be used to play the new sound.

SND_NOWAIT: If the driver is busy, return immediately without playing the sound.

SND_PURGE: Sounds are to be stopped for the calling task. If pszSound is not NULL, all instances of the specified sound are stopped. If pszSound is NULL, all sounds that are playing on behalf of the calling task are stopped.
You must also specify the instance handle to stop SND_RESOURCE events.

SND_RESOURCE: The pszSound parameter is a resource identifier; hmod must identify the instance that contains the resource.

SND_SYNC: Synchronous playback of a sound event. PlaySound returns after the sound event completes.

Esempio:

uses
MMSystem;

//Riproduzione singola

procedure TForm1.Button1.Click;
begin
PlaySound(’c:\eric.wav’, 0, SND_FILENAME + SND_ASYNC);
end;

//Riproduzione in loop

procedure TForm1.Button2.Click;
begin
PlaySound(’c:\eric.wav’, 0, SND_LOOP + SND_ASYNC);
end;

//Stop del loop

procedure TForm1.Button3.Click;
begin
PlaySound(nil, 0, 0);
end;

Compatibile con:

  • Windows XP: SI
  • Windows VISTA: SI
  • Windows 7: DA VERIFICARE

Leave a Reply