home *** CD-ROM | disk | FTP | other *** search
/ Delphi 4 Bible / Delphi_4_Bible_Tom_Swan_IDG_Books_1998.iso / source / VideoPlayer / Main.pas < prev    next >
Pascal/Delphi Source File  |  1998-03-11  |  4KB  |  104 lines

  1. unit Main;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, Buttons, MPlayer, ComCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     MediaPlayer1: TMediaPlayer;
  12.     OpenBitBtn: TBitBtn;
  13.     BitBtn2: TBitBtn;
  14.     OpenDialog1: TOpenDialog;
  15.     AutoPlayCheckBox: TCheckBox;
  16.     procedure OpenBitBtnClick(Sender: TObject);
  17.     procedure MediaPlayer1Notify(Sender: TObject);
  18.     procedure MediaPlayer1Click(Sender: TObject;
  19.       Button: TMPBtnType; var DoDefault: Boolean);
  20.     procedure AutoPlayCheckBoxClick(Sender: TObject);
  21.   private
  22.     { Private declarations }
  23.   public
  24.     { Public declarations }
  25.   end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. {$R *.DFM}
  33.  
  34. (* Note: Set the MediaPlayer1 object properties as follows
  35.      AutoEnable        False    All buttons always enabled
  36.      AutoOpen          True     Doesn't really matter
  37.      AutoRewind        True     Rewinds media when it stops
  38.      Other properties  Default settings
  39. *)
  40.  
  41. { Responds to user selection of the Open file button. }
  42. { Opens media file and starts playing immediately. }
  43. { Also sets the window caption to the filename. }
  44. procedure TForm1.OpenBitBtnClick(Sender: TObject);
  45. begin
  46.   if OpenDialog1.Execute then
  47.   begin
  48.     Form1.Caption := OpenDialog1.FileName;
  49.     MediaPlayer1.FileName := OpenDialog1.FileName;
  50.     MediaPlayer1.Notify := True;  // Wants media event notify
  51.     MediaPlayer1.Open;            // Opens assigned file
  52.     MediaPlayer1.Frames := 1;     // Sets single step frames
  53.     MediaPlayer1.Play;            // Start playing the file
  54.   end;
  55. end;
  56.  
  57. { Responds to media event notifications. This gives us the
  58.   chance to check whether the auto-replay checkbox is set, and
  59.   if so, and if the media is stopped, to restart play. Despite
  60.   the control's documentation, it is necessary to check whether
  61.   Notify is true. Even if this flag is false, the procedure is
  62.   called when the user clicks the stop button. }
  63. procedure TForm1.MediaPlayer1Notify(Sender: TObject);
  64. begin
  65.   if (MediaPlayer1.Notify) and            // If flag is true
  66.      (MediaPlayer1.Mode = mpStopped) and  // and play's stopped
  67.      (AutoPlayCheckBox.Checked) then      // & checkbox enabled
  68.   begin
  69.     MediaPlayer1.Rewind;                  // rewind to start
  70.     MediaPlayer1.Play;                    // and begin playing
  71.   end;
  72.   { You must set Notify to True so that the next media event
  73.     will generate a notification; otherwise, this procedure
  74.     would be called only once. }
  75.   MediaPlayer1.Notify := True;  // Request next notification
  76. end;
  77.  
  78. { Responds to user clicking in the MediaPlayer object. We need
  79.   to check for this because, if the auto-replay checkbox is
  80.   enabled, stopping the media would generate a notification,
  81.   which would start playing again! In other words, this
  82.   procedure allows the media to be stopped regardless of the
  83.   auto-replay checkbox setting. }
  84. procedure TForm1.MediaPlayer1Click(Sender: TObject;
  85.   Button: TMPBtnType; var DoDefault: Boolean);
  86. begin
  87.   if (Button = btStop) or (Button = btPause) then
  88.     MediaPlayer1.Notify := False   // Do not continue replay
  89.   else
  90.     MediaPlayer1.Notify := True;   // Replay if checkbox is set
  91. end;
  92.  
  93. { Responds to user changing the state of the auto-replay
  94.   checkbox. If the checkbox is being enabled, this procedure
  95.   turns on notifications so that, when the media stops, the
  96.   Notify event handler can restart the media playing. }
  97. procedure TForm1.AutoPlayCheckBoxClick(Sender: TObject);
  98. begin
  99.   if AutoPlayCheckBox.Checked then
  100.     MediaPlayer1.Notify := True;  // Triggers notifications
  101. end;
  102.  
  103. end.
  104.