Search code examples
audioinno-setuppascalscript

How to have a custom click sound on button click in Inno Setup (Back + Next + Cancel)?


How to have button click sounds in Inno setup?

I mean a different for "Back", "Next" and "Cancel".

I know there might be some questions and also answers to them, but I'm new to this site and I need some help.

Thanks in advance...


Solution

  • You can use Inno Media Player to play sounds.

    See question Playing sound during an Inno Setup install.

    To trigger the sound on button clicks use a code like:

    [Files]
    Source: "next.mp3"; Flags: dontcopy
    Source: "back.mp3"; Flags: dontcopy
    Source: "cancel.mp3"; Flags: dontcopy
    Source: "MediaPlayer.dll"; Flags: dontcopy
    
    [Code]
    
    type
      TDirectShowEventProc = procedure(EventCode, Param1, Param2: Integer);
    
    function DSInitializeAudioFile(
      FileName: string; CallbackProc: TDirectShowEventProc): Boolean;
      external 'DSInitializeAudioFile@files:mediaplayer.dll stdcall';
    function DSPlayMediaFile: Boolean;
      external 'DSPlayMediaFile@files:mediaplayer.dll stdcall';
    function DSStopMediaPlay: Boolean;
      external 'DSStopMediaPlay@files:mediaplayer.dll stdcall';
    
    function GetTickCount: DWORD;
      external 'GetTickCount@kernel32.dll stdcall';      
    
    procedure DeinitializeSetup;
    begin
      DSStopMediaPlay;
    end;
    
    var
      PageChanged: DWORD;
    
    procedure CurPageChanged(CurPageID: Integer);
    begin
      PageChanged := GetTickCount;
    end;
    
    procedure DirectShowEvent(EventCode, Param1, Param2: Integer);
    begin
      { dummy }
    end;
    
    procedure PlaySound(FileName: string);
    begin
      DSStopMediaPlay;
      ExtractTemporaryFile(FileName);
    
      if DSInitializeAudioFile(ExpandConstant('{tmp}\') + FileName, @DirectShowEvent) then
      begin
        DSPlayMediaFile;
      end;
    end;
    
    function NextButtonClick(CurPageID: Integer): Boolean;
    begin
      { NextButtonClick is called even for skipped pages (like the Welcome page) and }
      { during silent installs. To detect that, we check if at least half }
      { second elapsed since the page was shown }
      if GetTickCount - PageChanged > 500 then
      begin
        PlaySound('next.mp3');
      end;
      Result := True;
    end;
    
    function BackButtonClick(CurPageID: Integer): Boolean;
    begin
      PlaySound('back.mp3');
      Result := True;
    end;
    
    procedure CancelButtonClick(CurPageID: Integer; var Cancel, Confirm: Boolean);
    begin
      PlaySound('cancel.mp3');
    end;