2

I want to create two buttons adjacent to each-other at the bottom left corner of all the pages like reloaded.

The first button shows the Pause and the Resume command (if I click Pause, the button will switch to Resume automatically).

The other button shows the Stop command (if I click Stop, the next button will switch to Play from start automatically)

This code just adds Pause/Resume/Play from start* button and Stop button

The problem is: If I press Stop button, the music stops permanently and the Play form start button never appears.

    [Setup]
AppName=Bass Audio Project
AppVersion=1.0
DefaultDirName={pf}\Bass Audio Project

[Files]
Source: Bass.dll; Flags: dontcopy
Source: AudioFile.mp3; Flags: dontcopy


[Code]
const
  BASS_SAMPLE_LOOP = 4;
  BASS_ACTIVE_STOPPED = 0;
  BASS_ACTIVE_PLAYING = 1;
  BASS_ACTIVE_STALLED = 2;
  BASS_ACTIVE_PAUSED  = 3;
  BASS_UNICODE = $80000000;
  BASS_CONFIG_GVOL_STREAM = 5;
const
  #ifndef UNICODE
    EncodingFlag = 0;
  #else
    EncodingFlag = BASS_UNICODE;
  #endif
type
  HSTREAM = DWORD;

function BASS_Init(device: LongInt; freq, flags: DWORD;
  win: HWND; clsid: Cardinal): BOOL;
  external 'BASS_Init@files:bass.dll stdcall';
function BASS_StreamCreateFile(mem: BOOL; f: string; offset1: DWORD;
  offset2: DWORD; length1: DWORD; length2: DWORD; flags: DWORD): HSTREAM;
  external 'BASS_StreamCreateFile@files:bass.dll stdcall';
function BASS_Start: BOOL;
  external 'BASS_Start@files:bass.dll stdcall';
function BASS_Pause: BOOL;
  external 'BASS_Pause@files:bass.dll stdcall';
function BASS_Stop(): Boolean;
  external 'BASS_Stop@files:BASS.dll stdcall delayload';
function BASS_ChannelPlay(handle: DWORD; restart: BOOL): BOOL;
  external 'BASS_ChannelPlay@files:bass.dll stdcall';
function BASS_SetConfig(option: DWORD; value: DWORD ): BOOL;
  external 'BASS_SetConfig@files:bass.dll stdcall';
function BASS_ChannelIsActive(handle: DWORD): DWORD;
  external 'BASS_ChannelIsActive@files:bass.dll stdcall';
function BASS_Free: BOOL;
  external 'BASS_Free@files:bass.dll stdcall';

var
  SoundStream: HSTREAM;
  PauseResumePlayButton: TNewButton;
  StopButton: TNewButton;

procedure ResumeButtonClick(Sender: TObject); forward;

procedure PauseButtonClick(Sender: TObject);
begin
  if BASS_Pause then
  begin
    PauseResumePlayButton.Caption := 'Resume';
    PauseResumePlayButton.OnClick := @ResumeButtonClick;
  end;
end;

procedure ResumeButtonClick(Sender: TObject);
begin
  if BASS_start then
  begin
    PauseResumePlayButton.Caption := 'Pause';
    PauseResumePlayButton.OnClick := @PauseButtonClick;
  end;
end;

procedure PlayButtonClick(Sender: TObject);
begin
  if BASS_Start then
  begin
    PauseResumePlayButton.Caption := 'Pause';
    PauseResumePlayButton.OnClick := @PauseButtonClick;
  end;
end;


procedure StopButtonClick(Sender: TObject);
begin
  if BASS_Stop then
  begin
    PauseResumePlayButton.Caption := 'Play';
    PauseResumePlayButton.OnClick := @PlayButtonClick;
  end;
       begin
       BASS_Free;
       end;
end;

procedure InitializeWizard();
begin
  ExtractTemporaryFile('AudioFile.mp3');
  if BASS_Init(-1, 44100, 0, 0, 0) then
  begin
    SoundStream :=
      BASS_StreamCreateFile(
        False, ExpandConstant('{tmp}\AudioFile.mp3'), 0, 0, 0, 0,
        EncodingFlag or BASS_SAMPLE_LOOP);
    BASS_SetConfig(BASS_CONFIG_GVOL_STREAM, 2500);
    BASS_ChannelPlay(SoundStream, False);

    StopButton := TNewButton.Create(WizardForm);
    StopButton.Parent := WizardForm;
    StopButton.Left :=
      WizardForm.ClientWidth -
      WizardForm.CancelButton.Left - WizardForm.CancelButton.Width;
    StopButton.Top := WizardForm.CancelButton.Top;
    StopButton.Width :=  WizardForm.CancelButton.Width;
    StopButton.Height := WizardForm.CancelButton.Height;
    StopButton.Caption := 'Stop';
    StopButton.OnClick := @StopButtonClick;

    PauseResumePlayButton := TNewButton.Create(WizardForm);
    PauseResumePlayButton.Parent := WizardForm;
    PauseResumePlayButton.Left :=
      WizardForm.ClientWidth -
      WizardForm.NextButton.Left - WizardForm.NextButton.Width;
    PauseResumePlayButton.Top := WizardForm.CancelButton.Top;
    PauseResumePlayButton.Width :=  WizardForm.CancelButton.Width;
    PauseResumePlayButton.Height := WizardForm.CancelButton.Height;
    PauseResumePlayButton.Caption := 'Pause';
    PauseResumePlayButton.OnClick := @PauseButtonClick;
  end;
end;

procedure DeinitializeSetup;
begin
  BASS_Free;
end;
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
  • 2
    You cannot ask a specific user who has not participated in the thread to help. Be patient, and someone will help you when they can. If you can't wait, hire a contractor and pay them to figure it out for you. (It's always unclear to me why people want to play music from an installer - it's job is to **install software**, not to be a home entertainment center. You're introducing a dependency on Bass.DLL to be installed before your software, you're making the download much larger (wasting bandwidth), and making the install much slower, not even counting the extra work you're doing now.) – Ken White Sep 08 '16 at 22:21
  • the Bass.DLL is just a 33.5kb, not that much space. and the download is indeed large so a few kilobytes additial to the gigantic files won't hurt. the install would take at least 15 minutes to be installed, so it would be a good idea to play music during install – Muhammad Gibriel Sep 08 '16 at 22:35
  • 1
    It also has to be installed first, and you have to include all of the sound files as well. You can play music by including a couple of small .wav files and play them using the Windows MMAPI without requiring an install. Users don't need to pause, rewind, start and stop sounds during an install - that turns your installer into a media player. You're doing the equivalent of creating paint to put on things as you throw them in the garbage - they won't be around but a few minutes, but boy will they be pretty while they last! Use your installer to install. – Ken White Sep 09 '16 at 01:22

2 Answers2

1

It's not a good idea to implement this using a single OnClick handler.

Change the OnClick handler on the fly, as needed, for more straightforward code:

const
  BASS_SAMPLE_LOOP = 4;
  BASS_ACTIVE_STOPPED = 0;
  BASS_ACTIVE_PLAYING = 1;
  BASS_ACTIVE_STALLED = 2;
  BASS_ACTIVE_PAUSED  = 3;
  BASS_UNICODE = $80000000;
  BASS_CONFIG_GVOL_STREAM = 5;
const
  #ifndef UNICODE
    EncodingFlag = 0;
  #else
    EncodingFlag = BASS_UNICODE;
  #endif
type
  HSTREAM = DWORD;

function BASS_Init(device: LongInt; freq, flags: DWORD;
  win: HWND; clsid: Cardinal): Boolean;
  external 'BASS_Init@files:bass.dll stdcall';
function BASS_StreamCreateFile(mem: Boolean; f: string; offset1: DWORD;
  offset2: DWORD; length1: DWORD; length2: DWORD; flags: DWORD): HSTREAM;
  external 'BASS_StreamCreateFile@files:bass.dll stdcall';
function BASS_ChannelPlay(handle: DWORD; restart: Boolean): Boolean;
  external 'BASS_ChannelPlay@files:bass.dll stdcall';
function BASS_ChannelPause(handle: DWORD): Boolean;
  external 'BASS_ChannelPause@files:bass.dll stdcall';
function BASS_SetConfig(option: DWORD; value: DWORD ): Boolean;
  external 'BASS_SetConfig@files:bass.dll stdcall';
function BASS_ChannelIsActive(handle: DWORD): DWORD;
  external 'BASS_ChannelIsActive@files:bass.dll stdcall';

var
  SoundStream: HSTREAM;
  PauseResumePlayButton: TNewButton;
  StopButton: TNewButton;

procedure ResumeButtonClick(Sender: TObject); forward;

procedure PauseButtonClick(Sender: TObject);
begin
  if BASS_ChannelPause(SoundStream) then
  begin
    PauseResumePlayButton.Caption := 'Resume';
    PauseResumePlayButton.OnClick := @ResumeButtonClick;
  end;
end;  

procedure ResumeButtonClick(Sender: TObject);
begin
  if BASS_ChannelPlay(SoundStream, False) then
  begin
    PauseResumePlayButton.Caption := 'Pause';
    PauseResumePlayButton.OnClick := @PauseButtonClick;
  end;
end;  

procedure PlayButtonClick(Sender: TObject);
begin
  if BASS_ChannelPlay(SoundStream, True) then
  begin
    PauseResumePlayButton.Caption := 'Pause';
    PauseResumePlayButton.OnClick := @PauseButtonClick;
  end;
end;  

procedure StopButtonClick(Sender: TObject);
begin
  if (BASS_ChannelIsActive(SoundStream) = BASS_ACTIVE_PAUSED) or
     BASS_ChannelPause(SoundStream) then
  begin
    PauseResumePlayButton.Caption := 'Play';
    PauseResumePlayButton.OnClick := @PlayButtonClick;
  end;
end;  

procedure InitializeWizard();
begin
  if BASS_Init(-1, 44100, 0, 0, 0) then
  begin
    ExtractTemporaryFile('AudioFile.mp3');
    SoundStream :=
      BASS_StreamCreateFile(
        False, ExpandConstant('{tmp}\AudioFile.mp3'), 0, 0, 0, 0,
        EncodingFlag or BASS_SAMPLE_LOOP);

    if SoundStream <> 0 then
    begin
      BASS_SetConfig(BASS_CONFIG_GVOL_STREAM, 2500);

      if BASS_ChannelPlay(SoundStream, False) then
      begin
        StopButton := TNewButton.Create(WizardForm);
        StopButton.Parent := WizardForm;
        StopButton.Left :=
          WizardForm.ClientWidth -
          WizardForm.CancelButton.Left -  WizardForm.CancelButton.Width;
        StopButton.Top := WizardForm.CancelButton.Top;
        StopButton.Width :=  WizardForm.CancelButton.Width;
        StopButton.Height := WizardForm.CancelButton.Height;
        StopButton.Caption := 'Stop';
        StopButton.OnClick := @StopButtonClick;

        PauseResumePlayButton := TNewButton.Create(WizardForm);
        PauseResumePlayButton.Parent := WizardForm;
        PauseResumePlayButton.Left :=
          WizardForm.ClientWidth -
          WizardForm.NextButton.Left -  WizardForm.NextButton.Width;
        PauseResumePlayButton.Top := WizardForm.CancelButton.Top;
        PauseResumePlayButton.Width :=  WizardForm.CancelButton.Width;
        PauseResumePlayButton.Height := WizardForm.CancelButton.Height;
        PauseResumePlayButton.Caption := 'Pause';
        PauseResumePlayButton.OnClick := @PauseButtonClick;
      end;
    end;
  end;
end;
  • Note that you called the BASS_StreamCreateFile, BASS_SetConfig and BASS_ChannelPlay twice with the same arguments. It does not make sense. I've removed the second set.
  • I've also tweaked placement of the buttons.

Music control buttons

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
0
function BASS_ChannelPlay(handle: DWORD; restart: Boolean): Boolean;
  external 'BASS_ChannelPlay@files:bass.dll stdcall';
function BASS_ChannelPause(handle: DWORD): Boolean;
  external 'BASS_ChannelPause@files:bass.dll stdcall';
function BASS_ChannelIsActive(handle: DWORD): DWORD;
  external 'BASS_ChannelIsActive@files:bass.dll stdcall';

procedure MusicButtonOnClick(Sender: TObject);
begin
  if (BASS_ChannelIsActive(SoundStream) = 3)
 then
  BASS_ChannelPlay(SoundStream, False)
 else
  BASS_ChannelPause(SoundStream) 
end;

and on the button.OnClick add like this :

  YourButtonName.OnClick := @MusicButtonOnClick;
Death GOD 7
  • 63
  • 1
  • 6