Recent

Author Topic: Playing sound --- help---  (Read 14667 times)

albino

  • New member
  • *
  • Posts: 7
Playing sound --- help---
« on: April 07, 2008, 02:50:19 am »
Hello friends of lazarus forum:

I am a beginner (Windows) and I am trying to play sound from memory. I created a lrs file to store my  "wav files"  but I coudn't play that sounds.

I know I have to use playsound but I don't know how to do it . Please can some body help me with a detailed code?

Thank you.

OnixJr

  • Full Member
  • ***
  • Posts: 172
    • http://www.brlazarus.kit.net
RE: Playing sound --- help---
« Reply #1 on: April 07, 2008, 04:03:25 am »
First of all: welcome to Lazarus Forum =)

Aswering the question: Please can you put your code here?
Otherwise, nobody can help you without give you a complete source...

Trying a quick answer: Did you put MMSystem on uses clauses? (I'm thinking that you are running Lazarus under Windows)

Regards,
Júnior
Portal Lazarus Brasil - http://lazaruspascal.codigolivre.org.br/portal.php
Lazarus BOOK (in portuguese) - http://lazarus-book.blogspot.com
<hipernetjr@yahoo.com.br> - Curitiba/Brazil

albino

  • New member
  • *
  • Posts: 7
Sorry, my code.
« Reply #2 on: April 07, 2008, 10:03:39 pm »
Hello again, thanks for your answer and "sorry" here is my code. I tried this and it works:

uses MMSystem;

procedure TForm1.Button1Click(Sender: TObject);
begin
  sndPlaySound('C:\dog.wav',SND_NODEFAULT Or SND_ASYNC);
end;


Now my problem is loading a lrs file, I wrote this code but it doesn't work.

uses MMSystem;

initialization
{$I unit1.lrs}
{$I sounds.lrs}

procedure TForm1.Button1Click(Sender: TObject);

begin
  PlaySound('dog', hInstance, SND_RESOURCE or SND_SYNC);
end;


Thank you again.

OnixJr

  • Full Member
  • ***
  • Posts: 172
    • http://www.brlazarus.kit.net
RE: Sorry, my code.
« Reply #3 on: April 08, 2008, 01:21:53 am »
Hello,

AFAIK, PlaySound works with Windows Resources (.res) only.
If you want play wav files only under Windows, you can use Windows Resources in PlaySound...
Otherwise, you can download/install ACS for Lazarus (Audio Component Suite) to play wav files on cross-plataform (http://wiki.lazarus.freepascal.org/ACS)...

PS: I don't know if exists a way to convert LazarusResources (.lrs) to Windows Resources...

I did create a example to write lazarus resource to disk and play after. See (I had created a lazarus resource with C:\notify.wav file):

Code: [Select]

procedure TForm1.Button1Click(Sender: TObject);
const
  FileName = 'C:\notify.wav';
var
  Stream: TFileStream;
begin
  Stream := TFileStream.Create(FileName, fmCreate);
  with LazarusResources.Find('notify') do
    Stream.Write(PChar(Value)^, Length(Value));
  Stream.Free;
  sndPlaySound(FileName, SND_NODEFAULT or SND_ASYNC);
end;

initialization
  {$I unit1.lrs}
  {$I C:\notify.lrs}


Good Luck
Regards,
Júnior
Portal Lazarus Brasil - http://lazaruspascal.codigolivre.org.br/portal.php
Lazarus BOOK (in portuguese) - http://lazarus-book.blogspot.com
<hipernetjr@yahoo.com.br> - Curitiba/Brazil

albino

  • New member
  • *
  • Posts: 7
play sound
« Reply #4 on: April 08, 2008, 07:06:35 am »
Hello, thanks for your answers. Now I understand .lrs (lazarus) and .res (windows) files are very different. Thank you.

lazer

  • Full Member
  • ***
  • Posts: 215
Re: Playing sound --- help---
« Reply #5 on: October 13, 2022, 08:40:19 am »
This seamed to answer the question nicely but when I add MMSystem it fails to compile:

Quote
Fatal: Cannot find MMSytem ...

Has this been renamed now?

Thanks.

Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: Playing sound --- help---
« Reply #6 on: October 13, 2022, 04:58:02 pm »
There is a component known by the
package - online package manager
called PlaysoundPackage.

Install it.
It makes a control appear in the tab "LazControls" called "playsound".

Drop it on your form and it works "by itsself" (=intuitive to use).

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2054
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Sorry, my code.
« Reply #7 on: October 13, 2022, 05:14:35 pm »
Hello again, thanks for your answer and "sorry" here is my code. I tried this and it works:

uses MMSystem;

procedure TForm1.Button1Click(Sender: TObject);
begin
  sndPlaySound('C:\dog.wav',SND_NODEFAULT Or SND_ASYNC);
end;


Now my problem is loading a lrs file, I wrote this code but it doesn't work.

uses MMSystem;

initialization
{$I unit1.lrs}
{$I sounds.lrs}

procedure TForm1.Button1Click(Sender: TObject);

begin
  PlaySound('dog', hInstance, SND_RESOURCE or SND_SYNC);
end;


Thank you again.
For windows you can do it like here: (add waves in RCDATA section with built-in resource editor)
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   resStream: TResourceStream;
  4. begin
  5.   resStream := TResourceStream.Create(HINSTANCE, 'IDENTIFIER_NAME_OF_WAVE', RT_RCDATA);
  6.   try
  7.     PlaySound(PChar(resStream.Memory), 0, SND_MEMORY or SND_SYNC);
  8.   finally
  9.     resStream.Free;
  10.   end;
  11. end;
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

 

TinyPortal © 2005-2018