频道分类

Delphi MediaPlayer1 播放avi 视频

作者:admin 来源: 日期:2020/1/19 2:12:26 人气: 标签:

 
delphitop.com 提示 本示例演示了TMediaPlayer组件的用法,假设表单上存在两个“打开”和“停止”按钮。注意:为了能够手动控制按钮,必须将“自动播放”设置为“false”。
procedure TForm2.btOpenClick(Sender: TObject);
var
  OpenMediaDialog : TOpenDialog;
begin
  OpenMediaDialog := TOpenDialog.Create(Self);
  OpenMediaDialog.Filter := 'All Video Files (*.avi)|*.avi';
  // Browse for .avi files on your computer
  if OpenMediaDialog.Execute() then
  begin
    { Assign a file to the media player. }
    MediaPlayer1.FileName := OpenMediaDialog.FileName;

    { Check if the file exists and is not a directory. }
    if (FileExists(OpenMediaDialog.FileName)) and
       (not DirectoryExists(OpenMediaDialog.FileName)) then
    begin
      { Open the files. }
      MediaPlayer1.Wait := true;
      MediaPlayer1.Open;
      MediaPlayer1.Play;

      { Override automatic button controlling. }
      MediaPlayer1.EnabledButtons :=
        [TMPBtnType.btPause, TMPBtnType.btStop, TMPBtnType.btPlay];

      { Enable the Stop button. }
      btStop.Enabled := true;
      btOpen.Enabled := false;
    end;
  end;

  OpenMediaDialog.Free;
end;

procedure TForm2.btStopClick(Sender: TObject);
begin
  { Stop and close the media. }
  MediaPlayer1.Stop;
  MediaPlayer1.Close;

  MediaPlayer1.EnabledButtons := [];

  { Enable Open button again. }
  btOpen.Enabled := true;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  { Disable all buttons. }
  MediaPlayer1.AutoEnable := false;
  MediaPlayer1.EnabledButtons := [];
end;

procedure TForm2.MediaPlayer1PostClick(Sender: TObject;
  Button: TMPBtnType);
begin
  if Button = TMPBtnType.btStop then
     btStop.Click;
end;