Lazarus

Programming => Graphics and Multimedia => Audio and Video => Topic started by: SaraT on March 13, 2020, 07:12:51 pm

Title: SOLVED: Not sound on playing mp3 from resource
Post by: SaraT on March 13, 2020, 07:12:51 pm
Hello coders :)
I am trying to play a mp3 file from Laz resources but it does not work, like if it is muted.
Any help will be appreciated.

Greetings.

Code: Pascal  [Select][+][-]
  1. //The .mp3 file is type RCDATA, called MYSOUND in project options.
  2. var
  3.   S: TResourceStream;
  4. begin
  5.   S := TResourceStream.Create(HInstance, 'MYSOUND', RT_RCDATA);
  6.   try
  7.     PlaySound('MYSOUND', 0, SND_RESOURCE);
  8.   finally
  9.     S.Free;
  10.   end;
Title: Re: Not sound on playing mp3 from resource
Post by: Handoko on March 13, 2020, 07:25:36 pm
Maybe this can help:
https://forum.lazarus.freepascal.org/index.php?topic=46860.0
Title: Re: Not sound on playing mp3 from resource
Post by: winni on March 13, 2020, 07:36:46 pm
Hi!

PlaySound is only for wav files:

Code: Text  [Select][+][-]
  1. The object is to play a simple WAV sound both in Windows and Linux (Sync and ASync)

from:

https://wiki.freepascal.org/Play_Sound_Multiplatform (https://wiki.freepascal.org/Play_Sound_Multiplatform)


Winni
Title: Re: Not sound on playing mp3 from resource
Post by: lucamar on March 13, 2020, 07:54:01 pm
Note also that PlaySound() already implements the code needed to get to the ressource, so you don't need to create a resource stream. And, IIRC (sorry, has been years since I actively developed on Windows) you should pass to it the instance handle of the application.

All in all it should be just something like:

Code: Pascal  [Select][+][-]
  1. begin
  2.   if not PlaySound('MYSOUND', Application.Handle, SND_RESOURCE or SND_ASYNC) then
  3.     ShowMessage('Can''t play "MYSOUND"');
  4. end;

But as Winni said, AFAIR PlaySound() can only deal with RT_WAVE resources, not with generic RT_DATA ones.

Take all this, with a pinch of salt: I left Windows behind (except for testing) with the release of Vista ;)
Title: Re: Not sound on playing mp3 from resource
Post by: SaraT on March 13, 2020, 07:56:07 pm
Hi!

PlaySound is only for wav files:

Code: Text  [Select][+][-]
  1. The object is to play a simple WAV sound both in Windows and Linux (Sync and ASync)

from:

https://wiki.freepascal.org/Play_Sound_Multiplatform (https://wiki.freepascal.org/Play_Sound_Multiplatform)


Winni

Thx winni, I gave it a tried and works but the wav file is external.
I want to play a sound from resource.
Any idea/code? please

Thx
Title: Re: Not sound on playing mp3 from resource
Post by: Fred vS on March 13, 2020, 08:26:40 pm
Quote
I want to play a sound from resource.
Any idea/code? please

You may use uos https://github.com/fredvs/uos (https://github.com/fredvs/uos)

Take a look in uos/examples/ at consoleplaymemorystream.lpi.

Fre;D
Title: Re: Not sound on playing mp3 from resource
Post by: winni on March 13, 2020, 08:38:29 pm

Thx winni, I gave it a tried and works but the wav file is external.
I want to play a sound from resource.
Any idea/code? please

Thx

Have a look at Lucamars example.
That works fine!

Winni
Title: Re: Not sound on playing mp3 from resource
Post by: winni on March 13, 2020, 08:50:57 pm
@SaraT

Attached is a nice sound in lrs resource format.

The name is 'boat'

Winni
Title: Re: Not sound on playing mp3 from resource
Post by: Thaddy on March 13, 2020, 09:43:53 pm
Is the issue before
"Hello darkness, my old friend
I've come to talk with you again
Because a vision softly, creeping
Left its seeds while I was, sleeping
And the vision, that was planted in my brain... still remains
Within the sound of silence¨ or after? :-\
Title: Re: Not sound on playing mp3 from resource
Post by: winni on March 13, 2020, 09:48:51 pm
Yes!

My first song on the guitar when I was 11.
Damned F major nearly broke my little fingers!

Winni
Title: Re: Not sound on playing mp3 from resource
Post by: SaraT on March 14, 2020, 04:27:51 pm
Thx coders :)
I found a way to play a .wav file without any component. It works great!
Tested in a blank document.

Add the file as RCDATA in Project > Options > Resources.

Code: Pascal  [Select][+][-]
  1. //Needed in uses section
  2. uses
  3.   Windows, MMSystem;
  4.  
  5. //...
  6.  
  7. procedure TForm1.Button1Click(Sender: TObject);
  8. var
  9.   S: TResourceStream;
  10. begin
  11.   S := TResourceStream.Create(HInstance, 'DOORBELL', RT_RCDATA);
  12.   try
  13.     S.Position := 0;
  14.     SndPlaySound(S.Memory, SND_MEMORY or SND_ASYNC);
  15.   finally
  16.     S.Free;
  17.   end;
  18. end;

 ;)
Title: Re: SOLVED: Not sound on playing mp3 from resource
Post by: Handoko on March 14, 2020, 04:30:47 pm
Lazarus is awesome.
The more you learn it the more you will like it.
 ;D
TinyPortal © 2005-2018