unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    MediaPlayer1: TMediaPlayer;
    BitBtn1: TBitBtn;
    Panel1: TPanel;
    OpenDialog1: TOpenDialog;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
  private
  FileExt: string[4];
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
If OpenDialog1.Execute then
   Begin
     MediaPlayer1.Close;
     FileExt:=AnsiLowerCase(ExtractFileExt(OpenDialog1.FileName));//文件扩展名小写化
      if FileExt='.wav' then {是声音}
       begin
         Panel1.Caption:='播放声音';
         MediaPlayer1.Close;
         MediaPlayer1.FileName:=OpenDialog1.FileName;
         MediaPlayer1.Open;
         MediaPlayer1.Play;
       end
      else
         if FileExt='.avi' then{是动画}
           begin
             with MediaPlayer1 do
               begin
                  Close;
                  display:=Panel1;{播放窗口}
                  FileName:=OpenDialog1.FileName;
                  Panel1.Caption:='';
                  Open;
                  Rewind;
                  Play;
              end;
           end
         else
           if FileExt='.mid' then{是音乐}
               begin
                   Panel1.Caption:='播放音乐';
                   MediaPlayer1.Close;
                   MediaPlayer1.FileName:=OpenDialog1.FileName;
                   MediaPlayer1.Open;
                   MediaPlayer1.Play;
               end;
      end;
 end;

procedure TForm1.BitBtn2Click(Sender: TObject);
begin
   if OpenDialog1.FileName='' then
    begin
     MediaPlayer1.Close;
     MediaPlayer1.FileName:=OpenDialog1.FileName;
     MediaPlayer1.Open;
    end
End;

procedure TForm1.BitBtn3Click(Sender: TObject);
begin
Close;
end;

end.
