Recent

Author Topic: SOLVED: Not sound on playing mp3 from resource  (Read 2536 times)

SaraT

  • Full Member
  • ***
  • Posts: 121
  • A little student
SOLVED: Not sound on playing mp3 from resource
« 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;
« Last Edit: March 14, 2020, 04:28:15 pm by SaraT »

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Not sound on playing mp3 from resource
« Reply #1 on: March 13, 2020, 07:25:36 pm »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Not sound on playing mp3 from resource
« Reply #2 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


Winni

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Not sound on playing mp3 from resource
« Reply #3 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 ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

SaraT

  • Full Member
  • ***
  • Posts: 121
  • A little student
Re: Not sound on playing mp3 from resource
« Reply #4 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


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

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: Not sound on playing mp3 from resource
« Reply #5 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

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

Fre;D
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Not sound on playing mp3 from resource
« Reply #6 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

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Not sound on playing mp3 from resource
« Reply #7 on: March 13, 2020, 08:50:57 pm »
@SaraT

Attached is a nice sound in lrs resource format.

The name is 'boat'

Winni

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Not sound on playing mp3 from resource
« Reply #8 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? :-\
Specialize a type, not a var.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Not sound on playing mp3 from resource
« Reply #9 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

SaraT

  • Full Member
  • ***
  • Posts: 121
  • A little student
Re: Not sound on playing mp3 from resource
« Reply #10 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;

 ;)

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: SOLVED: Not sound on playing mp3 from resource
« Reply #11 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