Why is this FireMonkey TrackBar1.Tag used this way?

I lifted this code snippet from the FireMonkey MediaPlayerHD sample program that came with Delphi XE4. I've never seen the Tag property used this way before. What is the purpose of doing this?

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

Update: Here is the complete source code:

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

// 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.

Upvotes: 0

Views: 1064

Answers (1)

David Heffernan
David Heffernan

Reputation: 612864

The only way for the value of Tag to have any impact here would be if there is an event handler that responds to the change in Value. And for that event handler to switch behaviour on the value of Tag.

Typically you'd do that if you wanted a programmatic modification of the track bar value to be treated differently than a user modification.

To work out exactly what's going on you need to look for the other uses of Tag in the rest of the code. Without seeing the rest of the code (should have been in the question) I cannot verify that the explanation is as above but it's the only plausible explanation for that code in my view.

No matter what, that use of Tag is pretty lame. I'd always like to see a variable with a name that described its purpose.

Update

And now that the full code has been added, it can be seen that the use of Tag is exactly as described above.

Upvotes: 3

Related Questions