3

我希望使用 Inno Setup 为我的应用程序创建安装脚本,但我希望它在安装过程中播放声音文件,这可能吗?如果是的话,你能指出我正确的方向吗!

4

2 回答 2

11

1. Inno 媒体播放器

您可以使用Inno Media Player图书馆(对不起自我宣传)。以下是它用于播放存储为设置内临时文件的音频文件的示例。

请注意,Inno Media Player 是一个 Unicode 库,因此您只能将它与 Inno Setup 的 Unicode 版本一起使用,而不能与 ANSI 版本一起使用!不支持 ANSI 版本的 Inno Setup...!

[Setup]
AppName=Media Player Project
AppVersion=1.0
DefaultDirName={pf}\Media Player Project

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

[Code]
const
  EC_COMPLETE = $01;
type
  TDirectShowEventProc = procedure(EventCode, Param1, Param2: Integer);

function DSGetLastError(var ErrorText: WideString): HRESULT;
  external 'DSGetLastError@files:mediaplayer.dll stdcall';
function DSPlayMediaFile: Boolean;
  external 'DSPlayMediaFile@files:mediaplayer.dll stdcall';
function DSStopMediaPlay: Boolean;
  external 'DSStopMediaPlay@files:mediaplayer.dll stdcall';
function DSSetVolume(Value: LongInt): Boolean;
  external 'DSSetVolume@files:mediaplayer.dll stdcall';
function DSInitializeAudioFile(FileName: WideString; 
  CallbackProc: TDirectShowEventProc): Boolean; 
  external 'DSInitializeAudioFile@files:mediaplayer.dll stdcall';

procedure OnMediaPlayerEvent(EventCode, Param1, Param2: Integer); 
begin
  if EventCode = EC_COMPLETE then
  begin
    { playback is done, so you can e.g. play the stream again, play another }
    { one using the same code as in InitializeWizard (in that case would be }
    { better to wrap that in some helper function) or do just nothing }
  end;
end;

procedure InitializeWizard;
var
  ErrorCode: HRESULT;
  ErrorText: WideString;   
begin
  ExtractTemporaryFile('AudioFile.mp3');
  if DSInitializeAudioFile(ExpandConstant('{tmp}\AudioFile.mp3'), 
    @OnMediaPlayerEvent) then
  begin
    DSSetVolume(-2500);
    DSPlayMediaFile;
  end
  else
  begin
    ErrorCode := DSGetLastError(ErrorText);
    MsgBox('TDirectShowPlayer error: ' + IntToStr(ErrorCode) + '; ' + 
      ErrorText, mbError, MB_OK);
  end;
end;

procedure DeinitializeSetup;
begin
  DSStopMediaPlay;
end;

2. 低音音频库

或者您可以使用例如Bass Audio Library图书馆,它可免费用于非商业用途。例如,要使用该库播放无限循环,您可以使用如下脚本。

此脚本和库与 Inno Setup、ANSI 和 Unicode 的两个版本兼容。

[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_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_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_Free: BOOL;
  external 'BASS_Free@files:bass.dll stdcall';

procedure InitializeWizard;
var
  StreamHandle: HSTREAM;
begin
  ExtractTemporaryFile('AudioFile.mp3');
  if BASS_Init(-1, 44100, 0, 0, 0) then
  begin
    StreamHandle := 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(StreamHandle, False);
  end;
end;

procedure DeinitializeSetup;
begin
  BASS_Free;
end;
于 2012-09-10T23:26:34.600 回答
0

为避免需要第三方库,您还可以使用 Windows SAPI 对象。

[Files]
Source: "sound.wav"; Flags: dontcopy
var
  oVoice, oSpFileStream: Variant;
begin
  ExtractTemporaryFile('sound.wav');
  oVoice := CreateOleObject('SAPI.SpVoice');
  oSpFileStream := CreateOleObject('SAPI.SpFileStream');
  oSpFileStream.Open(ExpandConstant('{tmp}\sound.wav'));
  oVoice.SpeakStream(oSpFileStream);
end;
于 2022-02-23T10:53:56.500 回答