Recent

Author Topic: Help - Play Sound  (Read 27671 times)

albino

  • New member
  • *
  • Posts: 7
Help - Play Sound
« on: April 07, 2008, 02:55:41 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.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
RE: Help - Play Sound
« Reply #1 on: April 07, 2008, 02:13:14 pm »
I found http://msdn2.microsoft.com/en-us/library/ms712876(VS.85).aspx on msdn (in C).
Just convert it to Pascal, it won't be too difficult. The bad news, it uses windows resource. I'll try to find out the lazarus resource version at home.

albino

  • New member
  • *
  • Posts: 7
Sorry, my code.
« Reply #2 on: April 07, 2008, 09:52:25 pm »
Hello again, thanks for your answer. 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.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
RE: Sorry, my code.
« Reply #3 on: April 08, 2008, 10:44:34 am »
Just like I said, it uses windows resources (.rc / .res), not lazarus resources (.lrs). You can get the wav data as string with LazarusResources.Find('dog').Value. Find any other procedures / functions or classes that can represent the data as binary here (sorry, I must leave now).

rabiul

  • New member
  • *
  • Posts: 9
Help - Play Sound
« Reply #4 on: April 19, 2008, 06:32:12 pm »
How to do this on linux?
sndPlaySound('C:\dog.wav',SND_NODEFAULT Or SND_ASYNC); -->> linux equivalent??:?:

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
Help - Play Sound
« Reply #5 on: May 01, 2008, 01:42:47 pm »
This works in Windows:
PlaySound('c:\sounds\mysong.wav', 0, 0);

Troodon

  • Sr. Member
  • ****
  • Posts: 484
Re: Help - Play Sound
« Reply #6 on: April 06, 2009, 02:24:46 pm »
In Linux, to play a WAV file your Lazarus/FPC program must "talk to" the sound server (the audio mixer). There may be different sound servers installed depending on the distribution and desktop type (e.g., Gnome vs. KDE).

On Ubuntu 8.04 LTS (Hardy Heron), where the default desktop is Gnome, the sound server is PulseAudio. The pulseaudio-utils package includes the paplay utility that can play WAV files. Here is an example:

Code: [Select]
// for systems using the PulseAudio sound server
// such as Linux Ubuntu Hardy Heron

uses
  [...] FileUtil, Process;

function PlaySoundLnx(fileName: String): Boolean;
const
  playerCmd = 'paplay';  // pulseaudio client
var
  AProcess: TProcess;
begin
  AProcess := TProcess.Create(nil);
  with Aprocess do begin
    CommandLine := FindDefaultExecutablePath(playerCmd) +
      ' ' + filename;
    //Options := Options + [poWaitOnExit];
    try
      try
        Execute;
      except
        on E: Exception do
          ShowMessage(E.ClassName +
            ' error raised, with message : ' + E.Message);
      end;
    finally
      Free;
    end;
  end;
end;

[...]

procedure PlayMyWAV;
begin
  // get application directory;
  // wav file is in the adudio subdirectory
  AppDir := ExtractFilePath(Application.ExeName);
  // play WAV
  PlaySoundLnx(AppDir + 'audio/my.wav');
end;

The following simpler code may also work in PlaySoundLnx (instead of using TProcess) but I have not tried it:

Code: [Select]
SysUtils.ExecuteProcess(FindDefaultExecutablePath('paplay'),
  AppDir + 'audio/my.wav');


On OpenSUSE and SLED (Gnome desktop) the sound server is esd and there is another posting in this forum that shows how to access the esd library directly from Laz/FPC.

On KDE-2/3-based desktops the sound server is usually aRts. Output from any sound-playing utility can be redirected to the aRts daemon (artsd) using the artsdsm utilty. The following should work:

Code: [Select]
artsdsp -m <player> my.wav

or

artsdsp -q <player> my.wav

where <player> can be one of: play (sox); aplay (alsa-utils); mplayer; mpeg123; etc.

The KDE-4 desktop uses Phonon.

You may also wish to read about how "PulseAudio Tames the Linux Audio Zoo", Part 1 and 2.
« Last Edit: April 07, 2009, 05:50:49 pm by Troodon »
Lazarus/FPC on Linux

Troodon

  • Sr. Member
  • ****
  • Posts: 484
Re: Help - Play Sound
« Reply #7 on: May 07, 2009, 07:08:15 pm »
Interestingly, there is support for esound on both major desktop Linux distributions, i.e., Ubuntu and OpenSuse/SLED. So the FPC function for playing WAV files that uses the esound library works on both distros. Both Ubuntu 8/9 and OpenSuse/SLED 11 use the PulseAudio sound server, which has builtin esound support for backward compatibility. However, to develop a Lazarus application that plays a WAV file you need to install the libesd0-dev package. On Ubuntu 8 you may also need the libesd0-alsa package. On SLED-11 things get a little more complicated because libesd0-devel is not in the (default) SLED-11 repositories; you can get the rpm from the OpenSuse-11.1 GNOME2 repository. It requires alsa-devel, which is available from rpm.pbone.net (make sure you get the same version as your installed alsa package).
Lazarus/FPC on Linux

 

TinyPortal © 2005-2018