[fpc-pascal] Re: Playing sounds with standard components

silvioprog silvioprog at gmail.com
Wed Jun 13 03:04:33 CEST 2012


2012/6/12 Graeme Geldenhuys <graemeg.lists at gmail.com>:
> On 12 June 2012 16:17, silvioprog <silvioprog at gmail.com> wrote:
>>
>> So, how to play audio (wav/mp3) with only SDL framework in Free Pascal?
>
>
> Simply reading a SDL tutorial or two, will quickly let you understand
> the following code. TSoundEngine is just an convenience class we
> created to add extra sound events and possibly swap out sound library
> frameworks in future - without needing to change our application code.
> Simple design pattern practices to simply your application code and
> hide complexity.
>
> This is not the complete code, but is the gist of it.
>
>
> uses
>  sdl_mixer,
>  sdl;
>
> const
>  // Global default extension for sound files. Set up as needed.
>  SoundExt           = '.wav';
>
> type
>
>  PSoundBlock = {$IFDEF UseMusic}PMIX_Music{$ELSE}PMIX_Chunk{$ENDIF};
>
>
>  ...
>  { in our project, this code is in a TSoundPlayer class. Result is of
> type PSoundBlock }
>    Result := {$IFDEF
> UseMusic}Mix_LoadMUS{$ELSE}Mix_LoadWAV{$ENDIF}(PChar(aFilename));
>    if not Assigned(Result) then
>      SoundEngine.MixError;
>  ...
>
>
>
> constructor TSoundEngine.Create;
> begin
>  if SDL_Init(SDL_INIT_AUDIO or SDL_INIT_TIMER) = -1 then
>    SDLError;
>  if Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT,
> MIX_DEFAULT_CHANNELS, 1024) = -1 then
>    MixError;
>  {$IFDEF UseMusic}Mix_HookMusicFinished{$ELSE}Mix_ChannelFinished{$ENDIF}(@MusicFinished);
> end;
>
> destructor TSoundEngine.Destroy;
> begin
>  PlayingStop;
>  MIX_CloseAudio;
>  SDL_Quit;
>  inherited Destroy;
> end;
>
> procedure TSoundEngine.PlayingStop;
> begin
>  {$IFDEF UseMusic}
>  Mix_HaltMusic;
>  {$ELSE}
>  Mix_HaltChannel(1);
>  {$ENDIF}
> end;
>
> procedure TSoundEngine.PlayingStart(aMusic: PSoundBlock);
> begin
>  {$IFDEF UseMusic}
>  if Mix_PlayMusic(aMusic, 0) <> 0 then
>    MixError;
>  {$ELSE}
>  Mix_PlayChannel(1,aMusic, 0)
>  {$ENDIF}
> end;
>
> procedure TSoundEngine.PlayingPause;
> begin
>  {$IFDEF UseMusic}
>  Mix_PauseMusic;
>  {$ELSE}
>  Mix_Pause (1);
>  {$ENDIF}
> end;
>
> procedure TSoundEngine.PlayingContinue;
> begin
>  {$IFDEF UseMusic}
>  Mix_ResumeMusic;
>  {$ELSE}
>  Mix_Resume (1);
>  {$ENDIF}
> end;
>
>
>
> --
> Regards,
>   - Graeme -

Hellow Graeme! :)

I'll try to test it tomorrow and return here with the results ...

Thank you in advance guy! :)

-- 
Silvio Clécio
My public projects - github.com/silvioprog



More information about the fpc-pascal mailing list