用DELPHI播放WAV和MP3

用于某企业知识竞赛,为了更有现场感,要加入音效。

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, MPlayer, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    MediaPlayer1: TMediaPlayer;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses mmsystem;

//播放一次wav
procedure TForm1.Button1Click(Sender: TObject);
var
  wavfile : String;
begin
  wavfile := '回答正确.wav';
  sndplaysound(pchar(wavfile),snd_sync);
  beep;

end;

//循环播放wav
procedure TForm1.Button2Click(Sender: TObject);
var
  wavfile : String;
begin
  wavfile := '回答错误.wav';
  sndplaysound(pchar(wavfile),SND_ASYNC or SND_LOOP);
  beep;
end;

//播放MP3
procedure TForm1.Button3Click(Sender: TObject);
begin
  MediaPlayer1.FileName := '上场音乐.mp3';
  MediaPlayer1.Open;
  MediaPlayer1.Play;
end;

end.


你可能感兴趣的:(DELPHI,音频)