Recent

Author Topic: (Solved)what are the steps to include a sound in Lazarus and Play It cross?  (Read 502 times)

lestroso

  • Jr. Member
  • **
  • Posts: 90
    • FaSasoftware
Dear friends,

i'm so sorry... i need to know how to include a file sound as resorce, or better i know how  to include it( inside Lazarus looking for Resource tab) as resource, but i don't know how to play inside lazarus...i have tryed many solution but without success...

I don't know if i must have a library..but which??working easy, for Mac and windows...at least and linux ok....

Why is so hard working on lazarus??? i have tryed Tplaysound....but in mac os ...need a direct path sound to work ...but i need a relative path...

So, for these reasons...i need please a step by step  help to understand the use of lazarus resources as sound and play them...

I thank you a lot in advance!
Best Regards,
Lestroso :-[
« Last Edit: August 20, 2019, 06:46:33 pm by lestroso »

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7354
Why is so hard working on lazarus???

Compared to which general purpose language?

jamie

  • Hero Member
  • *****
  • Posts: 1897
I don't think Mac as resource files attached to the EXE file like others do.

 You have a resource folder for your app and all items go there from what I understand.

 So as I understand your post I believe your issue is that you can not use a Resource ID t o play sound samples?

 If that being the case you'll need to modify your code using a proxy function that checks for OS and compile in the correct code via compiler switches

 As far as Lazarus being hard, it is a big deal to common code to work across platforms all the same way.

lucamar

  • Hero Member
  • *****
  • Posts: 1984
[...]....but in mac os ...need a direct path sound to work ...but i need a relative path...

Have you tried CreateAbsolutePath() and CreateRelativePath(), from the unit LazFileUtils (LazUtils package)?

That should allow you to convert from an absolute to a relative path or viceversa without problems.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

trev

  • Full Member
  • ***
  • Posts: 185
Quote
but in mac os ...need a direct path sound to work ...but i need a relative path...

I'm a little lost understanding your shorthand, but are you asking how to find the macOS app bundle Resources directory?
o Lazarus v2.1.0 r61775, FPC v3.3.1 r42640, macOS 10.14.6 (with sup update), Xcode 10.3
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.0 (Parallels VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)

lestroso

  • Jr. Member
  • **
  • Posts: 90
    • FaSasoftware
Dear Friends,

I know perfectly where is located the resource folder in mac os x......but to remain cross-platform...i need how to load and play sounds as resource in Lazarus...either in Mac os x, windows10 and linux...  I know..to include a resource in app or exe as sound ... i must go in the tab project and select a sound file as source RCDATA...and then???

Could somebody help me with step by step solution please????  I'm so sorry..i'm a beginner...Happy Summer, best regards,

Lestroso :-[

lucamar

  • Hero Member
  • *****
  • Posts: 1984
This is copy/pasted from a test project showing how to load a resource. Inthis case it's a RT_HTML resource loaded to a TMemo:

Code: Pascal  [Select]
  1. procedure TForm1.btLoadClick(Sender: TObject);
  2. var
  3.   ResStream: TResourceStream;
  4. begin
  5.   ResStream := TResourceStream.Create(HINSTANCE, 'BURNETTFH', RT_HTML);
  6.   try
  7.     Memo.Lines.LoadFromStream(ResStream);
  8.     {ResStream.SaveToFile('test.html');}
  9.   finally
  10.     ResStream.Free;
  11.   end;
  12. end;

And this one, from another program, creates a TINIFile from an RT_RCDATA resource.

Code: Pascal  [Select]
  1. function TMainForm.IniFromRes(const ResName: String): TIniFile;
  2. var
  3.   ResStream: TResourceStream;
  4. begin
  5.   ResStream := TResourceStream.Create(HINSTANCE, ResName, RT_RCDATA);
  6.   try
  7.     Result := TIniFile.Create(ResStream, [ifoStripQuotes]);
  8.   finally
  9.     ResStream.Free;
  10.   end;
  11. end;

As you can see, the basic lesson from those is to create a TResourceStream which can then be used by any class whose contents can be loaded from a stream.

I don't know whether your "play sound" class(es) can do it, but if it/they can, that is the basic way to do it.

HTH!
« Last Edit: August 19, 2019, 09:37:18 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

wp

  • Hero Member
  • *****
  • Posts: 6140
The attached demo program reads two audio files from the program's resources and plays them using the cross-platform TPlaySound component.

Step 1: Creation of the resource file
I usually write a small batch file (I am on Windows) which calls the tool "lazres" to add the resource items. Its sources are found in the folder "tools" of the Lazarus installation. Compile it and copy the exe somewhere so that it is easily found. The first parameter is the name of the resource file to be created; the following parameters are the names of the files to be added to the resource (in case of a long list of file names, the file names can be put into a separate text file which is mentioned here with a preceding @). A typical syntax is
Code: [Select]
lazres resource.res sound1.wav sound2.wavor
Code: [Select]
lazres resource.res @filelist.txtwith "filelist.txt" containing
Code: [Select]
sound1.wav
sound2.wav
This creates a resource file named "resource.res" containing two items "sound1" and "sound2" (the extension is dropped for the resource item name). These items have resource type RT_RCDATA

Step 2: Extract the resource
In the program code link the resource file by adding a line {$R resource.res} containing the resource file name. Specify the (relative) path if the resource file is not in the project folder directly.

In order to extract a resource item, e.g. "sound1" create a TResourceStream and specify the name in the call:
Code: Pascal  [Select]
  1. var
  2.   stream: TResourceStream;
  3. ...
  4.   stream := TResourceStream.Create(HINSTANCE, 'sound1', RT_RCDATA);
'sound1' is the name of the resource to be extracted (see step 1), RT_RCDATA is the type of the resource.

Step 3: Play the sound
Unfortunately, the PlaySound component has no direct stream interface. Therefore the stream must be written to a temporary file
Code: Pascal  [Select]
  1. var
  2.   tmpFileName: String;
  3. ...
  4.   tmpFileName :=   tmpFileName := AppendPathDelim(GetTempDir) + 'sound1.tmp';
  5.   stream.SaveToFile(tmpFileName);

In case of the TPlaySound component, the temporary filename must be passed to the property "SoundFile"; in order the play the sound, the method "Execute" must be called.

My demo contains a modified component unit which is usable without any further installation.

Code: Pascal  [Select]
  1. uses
  2.   uPlaySound;
  3.  
  4. var
  5.   player: TPlaySound;
  6. ...
  7.   player := TPlaySound.Create(nil);
  8.   try
  9.     player.SoundFile := tmpFileName;
  10.     player.Execute;
  11.   finally
  12.     player.Free;
  13.   end;  

Step 4: Clean up
When the program ends delete the temporary files (unless you want to keep them).

P.S.
Unfortunately, the resource file is too large for the forum, therefore I am posting only the program sources. Please send me a PM with your e-mail address and I'll return the full project with resource file.


« Last Edit: August 20, 2019, 01:03:25 am by wp »
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

mas steindorff

  • Sr. Member
  • ****
  • Posts: 425
nice write-up wp, this one goes into my tips folder!
windows 7/10 - laz 2.0 / 1.2.6 general releases

lestroso

  • Jr. Member
  • **
  • Posts: 90
    • FaSasoftware
Thanks to everybody...in this hours i'll try your solutions...and then i 'll tell you if i have  had success.... i thank you a lot again...Best regards Lestroso :-[

lestroso

  • Jr. Member
  • **
  • Posts: 90
    • FaSasoftware
Dear WP!!! You solved in Full My Problem!!! I have followed step by step your Teory..and it's works fine!!! Thanks again for the file...must be an example for all forum!

Finally, a real solution to solve the sounds problems! cross platform!!!

I have had a little problem in Mac os x to create the resource file..the compiler didn't go ...well....so i have had to make that file in windows and then put it in mac....Worked very fine....

Again...there's no words to thank you!!

Best regards,

Lestroso :D