0

我从 Delphi XE4 附带的 FireMonkey MediaPlayerHD 示例程序中提取了这个代码片段。我以前从未见过以这种方式使用 Tag 属性。这样做的目的是什么?

procedure TForm240.Timer1Timer(Sender: TObject);
begin
  TrackBar1.Tag := 1;
  TrackBar1.Value := MediaPlayer1.CurrentTime;
  TrackBar1.Tag := 0;
end;

更新: 这是完整的源代码:

//---------------------------------------------------------------------------

// This software is Copyright (c) 2012 Embarcadero Technologies, Inc. 
// You may only use this software if you are an authorized licensee
// of Delphi, C++Builder or RAD Studio (Embarcadero Products).
// This software is considered a Redistributable as defined under
// the software license agreement that comes with the Embarcadero Products
// and is subject to that software license agreement.

//---------------------------------------------------------------------------
unit MediaPlayerForm;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.Objects, FMX.Layouts, FMX.Media,
  FMX.Filter, FMX.Filter.Effects, FMX.StdCtrls;

type
  TForm240 = class(TForm)
    OpenDialog1: TOpenDialog;
    OpenButton: TSpeedButton;
    Rectangle1: TRectangle;
    PauseButton: TSpeedButton;
    TrackBar1: TTrackBar;
    Label1: TLabel;
    Timer1: TTimer;
    CheckBox1: TCheckBox;
    VolumeTrack: TTrackBar;
    MediaPlayer1: TMediaPlayer;
    MediaPlayerControl1: TMediaPlayerControl;
    ClearButton: TSpeedButton;
    procedure OpenButtonClick(Sender: TObject);
    procedure PauseButtonClick(Sender: TObject);
    procedure TrackBar1Change(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure CheckBox1Change(Sender: TObject);
    procedure VolumeTrackChange(Sender: TObject);
    procedure ClearButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form240: TForm240;

implementation

{$R *.fmx}

procedure TForm240.CheckBox1Change(Sender: TObject);
begin
  MediaPlayerControl1.Visible := CheckBox1.IsChecked;
end;

procedure TForm240.OpenButtonClick(Sender: TObject);
begin
  OpenDialog1.Filter := TMediaCodecManager.GetFilterString;
  if OpenDialog1.Execute then
  begin
    PauseButton.Enabled := True;
    ClearButton.Enabled := True;
    MediaPlayer1.FileName := OpenDialog1.FileName;

    if MediaPlayer1.Media <> nil then
    begin
      Label1.Text := IntToStr(MediaPlayer1.Media.VideoSize.Truncate.X) + 'x' + IntToStr(MediaPlayer1.Media.VideoSize.Truncate.Y) + 
        'px ' + IntToStr(MediaPlayer1.Media.Duration div MediaTimeScale) + 'ms';
      TrackBar1.Max := MediaPlayer1.Media.Duration;
      VolumeTrack.Value := 1 - MediaPlayer1.Media.Volume;

      MediaPlayer1.Play;
    end;
  end;
end;

procedure TForm240.PauseButtonClick(Sender: TObject);
begin
  if MediaPlayer1.State = TMediaState.Playing then
  begin 
    PauseButton.Text := 'Play';
    MediaPlayer1.Stop;
  end
  else
  begin
    PauseButton.Text := 'Pause';
    MediaPlayer1.Play;
  end;
end;

procedure TForm240.ClearButtonClick(Sender: TObject);
begin
  MediaPlayer1.Clear;
  PauseButton.Enabled := False;
  ClearButton.Enabled := False;
end;

procedure TForm240.Timer1Timer(Sender: TObject);
begin
  TrackBar1.Tag := 1;
  TrackBar1.Value := MediaPlayer1.CurrentTime;
  TrackBar1.Tag := 0;
end;

procedure TForm240.TrackBar1Change(Sender: TObject);
begin
  if TrackBar1.Tag = 0 then
    MediaPlayer1.CurrentTime := Round(TrackBar1.Value);
end;

procedure TForm240.VolumeTrackChange(Sender: TObject);
begin
  MediaPlayer1.Volume := 1 - VolumeTrack.Value; 
end;

end.
4

1 回答 1

3

Tag 的值在这里产生任何影响的唯一方法是如果有一个事件处理程序来响应 Value 的变化。并让该事件处理程序在 Tag 的值上切换行为。

通常,如果您希望以不同于用户修改的方式处理轨迹栏值的编程修改,您会这样做。

要准确了解发生了什么,您需要在其余代码中查找 Tag 的其他用途。在没有看到其余代码(应该在问题中)的情况下,我无法验证解释是否如上,但在我看来,这是该代码唯一合理的解释。

无论如何,Tag 的使用是相当蹩脚的。我总是希望看到一个具有描述其用途的名称的变量。

更新

并且现在已经添加了完整的代码,可以看出Tag的使用和上面描述的完全一样。

于 2013-07-14T21:57:45.150 回答