Recent

Author Topic: How to play sound with various path/names?  (Read 4660 times)

petic537

  • Newbie
  • Posts: 3
How to play sound with various path/names?
« on: November 13, 2014, 08:55:55 pm »
Hello, I would like to know how play sounds (wav files) with various names in Lazarus.
I have many sounds in my folder (sample1.wav , sample2.wav , sample3.wav). I need to select specific sound to play.

My code example:

path_and_name: string;

path_and_name:='folder\sample' + IntToStr(i) + '.wav';
PlaySound(path_and_name, 0, SND_ASYNC);

This code does not work. Could you please help me to solve this problem...

Petr

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: How to play sound with various path/names?
« Reply #1 on: November 13, 2014, 11:08:31 pm »
Quote
This code does not work. Could you please help me to solve this problem...

What kind of error do you get, what os are you using, what Lazarus version, what fpc version,... ?

Maybe, you could begin with the beginning...
Is it working if you use the full path for one wav only ? => something like :

Code: [Select]
PlaySound('C:\MyWavFolder\sample1.wav', 0, SND_ASYNC);
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

petic537

  • Newbie
  • Posts: 3
Re: How to play sound with various path/names?
« Reply #2 on: November 14, 2014, 07:19:50 pm »
Hello, this code works fine:
Code: [Select]
PlaySound('C:\MyWavFolder\sample1.wav', 0, SND_ASYNC);I have Lazarus 1.2.6 and fpc 2.6.4.

When I try to compile this code:
Code: [Select]
path_and_name:='folder\sample' + IntToStr(i) + '.wav';
PlaySound(path_and_name, 0, SND_ASYNC);

I got the following error message:

unit1.pas(236,20) Error: Incompatible type for arg no. 1: Got "AnsiString", expected "PChar"

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: How to play sound with various path/names?
« Reply #3 on: November 14, 2014, 07:41:45 pm »
=>
Quote
PlaySound(PChar(path_and_name), 0, SND_ASYNC);
« Last Edit: November 14, 2014, 07:47:35 pm by Fred vS »
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

petic537

  • Newbie
  • Posts: 3
Re: How to play sound with various path/names?
« Reply #4 on: November 14, 2014, 08:27:22 pm »
Thank you. Is there some way how convert string to pchar?

minesadorada

  • Sr. Member
  • ****
  • Posts: 452
  • Retired
Re: How to play sound with various path/names?
« Reply #5 on: November 14, 2014, 09:22:12 pm »
If you want to play .wav files in both Windows and Linux this page may help:

http://wiki.freepascal.org/Play_Sound_Multiplatform
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: How to play sound with various path/names?
« Reply #6 on: November 15, 2014, 12:22:06 pm »
Quote
Thank you. Is there some way how convert string to pchar?

Hum, => Pchar('MyString') must work... => if not, there are problems elsewhere in your code...

So, a good idea is to give us a litle more of your code...
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

 

TinyPortal © 2005-2018