PDA

View Full Version : I need a component in Lazarus to play a WAV file.



peter_from_hobart
27-05-2007, 03:17 PM
Hi All,

I'm porting a Delphi 7 program to Lazarus and I cannot find a TMediaPlayer component. I need a MediaPlayer-like component so that I can play Micro$oft Windows .WAV (sound wave files).

Below is the code I'm trying to convert to Lazarus:


var
MediaPlayer1 : TMediaPlayer;
....
procedure TMainForm.play_wave(sound_effect_wave_filename: string);

begin
if Want_Sound then
begin
// note: this 'if' test has deliberately NOT been
// combined with the above 'if Want_sound' test
// so that we only check 'if FileExists' when we want sound effects.
// IOW - don't check FileExists if we don't want sound effects.
if FileExists(sound_effect_wave_filename) then
begin
with MediaPlayer1 do
begin
filename := sound_effect_wave_filename;
AutoRewind := true;
Open;
Wait := true;
Play;
Close;
end;
end;
end;
end;

Any help will be appreciated.

Yours Sincerely and With Best Wishes,
pew { Peter_from_Hobart }

signing off from Hobart, Tasmania, Australia
(in Winter time here)

savage
27-05-2007, 04:58 PM
Hi Peter,
I'm not aware of a built in TMediaPlayer component for Lazarus. I know that if you use SDL via JEDI-SDL you could certainly write one. Actually the JEDI-SDL CVS contains a SDL_Mixer class wrapper that could be used to load and play Wavs and other sound media.

I hope this helps.

peter_from_hobart
28-05-2007, 03:16 AM
Hi Savage,

Savage wrote:

> I'm not aware of a built in TMediaPlayer component for Lazarus. I know that if you use SDL via JEDI-SDL you could certainly write one. Actually the JEDI-SDL CVS contains a SDL_Mixer class wrapper that could be used to load and play Wavs and other sound media.

okay... this project is getting bigger that Ben Hurr!!!

Can you please tell me the URL for me to get the JEDI-SDL CVS including the SDL_Mixer class wrapper that you mentioned would be able to play the WAV files. Also, I really need example programs showing me how to do it.

Yours Sincerely,
pew { peter_from_hobart }

Hobart, Tasmania, Australia.

Chebmaster
28-05-2007, 06:08 AM
this 'if' test has deliberately NOT been
// combined with the above 'if Want_sound' test
AFAIK, the compiler generates such code that it stops evaluating the boolean expression once its result is certain.

So, the construct

var s: ansistring;
...
if Assigned(s) and (s[1] = 'ы') then .....
is absolutely safe, since s[1] check is never performed if the string is empty.


So, procedure TMainForm.play_wave(sound_effect_wave_filename: string);
begin
if Want_Sound and FileExists(sound_effect_wave_filename)
then with MediaPlayer1 do
begin
filename := sound_effect_wave_filename;
AutoRewind := true;
Open;
Wait := true;
Play;
Close;
end;
end;

savage
28-05-2007, 09:35 AM
The URL to the audio mixer class is...
http://jedi-sdl.cvs.sourceforge.net/jedi-sdl/JEDI-SDLv1.0/SDL_Mixer/Pas/sdlaudiomixer.pas?view=log

Revision 1.4 is the latest version.

Within sdlaudiomixer.pas are several classes you can use...
TSDLSoundEffect - Handles just 1 sound effect
TSDLSoundEffectManager - Handles x sound effects
TSDLMusic - Handles just 1 music file
TSDLMusicManager - Handles x music files
TSDLAudioManager - Handle collections of both SoundEffects and Music files.

The SoundEffect and Music classes all support
Play
Stop
Pause
Resume
Rewind
as well as setting the Volume and Fading In and out.
You can also LoadFromFile. Though LoadFromStream exists as a method, these are not implemented yet.

A typical way of using TSDLSoundEffect or TSDLMusic on it's own would be ..
var
MySoundEffect : TSDLSoundEffect;

begin
MySoundEffect : TSDLSoundEffect.Create('<path>\MyWavFile.wav');
MySoundEffect.Play;
end;

The Manager classes allows you to add several SoundEffect or Music classes and then refer to them by name or index after they have been added to their respective lists...

MyAudoManager.SoundEffectNames['explosion'].Play;
or
MyAudoManager.TrackNames['background'].Play;

Note in order to compile and use this you will also need the other sdl related files that these classes are built from. Namely
sdl_mixer - http://jedi-sdl.cvs.sourceforge.net/jedi-sdl/JEDI-SDLv1.0/SDL_Mixer/Pas/sdl_mixer.pas?view=log

sdl - http://jedi-sdl.cvs.sourceforge.net/jedi-sdl/JEDI-SDLv1.0/SDL/Pas/sdl.pas?view=log

smpeg - http://jedi-sdl.cvs.sourceforge.net/jedi-sdl/JEDI-SDLv1.0/smpeg/Pas/smpeg.pas?view=log