明辉站/网站教程/内容

不用MediaPlayer播放Midi

网站教程2024-06-14 阅读
[摘要]你厌倦MediaPlayer吗?用了它你的EXE会大7K。 uses Windows, Classes, Forms, MMSystem, WinProcs; var wDeviceID: Word; PlayWindow: HWnd; procedure OpenMCI(PWindo...
你厌倦MediaPlayer吗?用了它你的EXE会大7K。
uses
  Windows, Classes, Forms, MMSystem, WinProcs;

var
  wDeviceID: Word;
  PlayWindow: HWnd;

procedure OpenMCI(PWindow: HWnd; FileName, DeviceType: PChar);
var
  OpenParms: Tmci_Open_Parms;
  Style: LongInt;
begin
  PlayWindow := PWindow;
  OpenParms.lpstrDeviceType := DeviceType;
  OpenParms.lpstrElementName := FileName;
  Style := Mci_Open_Type or Mci_Open_Element;
  mciSendCommand(0, MCI_OPEN, Style, LongInt(@OpenParms));
  wDeviceID := OpenParms.wDeviceID;
end;

procedure PlayMCI;
var
  Info: TMci_Play_Parms;
begin
  Info.dwCallback := PlayWindow;
  mciSendCommand(wDeviceID, MCI_PLAY, Mci_Notify, LongInt(@Info));
end;

procedure MidiPlay(MidiName: PChar);
const
  DevType: PChar='Sequencer';
begin
  if MidiName = 'StopMidi' then
    CloseMCI
  else
  begin
    OpenMci(Application.Handle, MidiName, DevType);
    PlayMci;
  end;
end;

procedure CloseMCI;
begin
  mciSendCommand(wDeviceID, MCI_CLOSE, 0, 0);
  wDeviceID := 0;
end;

end.

使用方法:
MidiPlay("Feeling.mid")); // 注意,这里必须是 PChar 或 char*
MidiPlay("StopMidi"); 

……

相关阅读