Mirror

How to play sound from a resource file (2) (Views: 100)


Problem/Question/Abstract:

How do I play an audio waveform from a memory buffer without writing to disk?

Answer:

procedure TFrmWaves.PlayTheSound(AName: PChar);
var
  resInfo: THandle;
  wavHdl: THandle;
  wavPtr: Pointer;
begin
  case RgrMethod.ItemIndex of
    0:
      begin
        resInfo := FindResource(HInstance, AName, RES_TYPE);
        if resInfo <> 0 then
        begin
          Label1.Caption := StrPas(AName);
          wavHdl := LoadResource(HInstance, resInfo);
          wavPtr := LockResource(wavHdl);
          sndPlaySound(wavPtr, SND_ASYNC or SND_MEMORY);
          if (wavHdl <> 0) then
          begin
            UnlockResource(wavHdl);
            FreeResource(wavHdl);
          end;
        end
        else
          Label1.Caption := StrPas(AName) + ': Not found';
      end;
    1: PlaySound(AName, HInstance, SND_RESOURCE);
  end;
end;

<< Back to main page