home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue148 / delphi / copydelph.exe / MPlay / MPUnit.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1998-10-20  |  3.1 KB  |  119 lines

  1. unit MPUnit;
  2. { PC Plus Sample Program.
  3.   A Simple Media Player for Delphi 2.0
  4.  
  5.   Plays Media files: AVI, WAV, MID, RMI etc.
  6.                      also CD Audio disks.
  7.  
  8.   Limitations:       Needs more error checking.
  9.                      Does not handle empty CD drive or non-Audio CDs.
  10.                      No display of track information (e.g. position) 
  11.  
  12.   Author: Huw Collingbourne
  13.   }
  14.  
  15. interface
  16.  
  17. uses
  18.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  19.   StdCtrls, MPlayer, Menus;
  20.  
  21. type
  22.   TForm1 = class(TForm)
  23.     MediaPlayer1: TMediaPlayer;
  24.     Label1: TLabel;
  25.     OpenDialog1: TOpenDialog;
  26.     MainMenu1: TMainMenu;
  27.     File1: TMenuItem;
  28.     Open1: TMenuItem;
  29.     Exit1: TMenuItem;
  30.     PlayCD1: TMenuItem;
  31.     procedure FormActivate(Sender: TObject);
  32.     procedure MediaPlayer1Click(Sender: TObject; Button: TMPBtnType;
  33.       var DoDefault: Boolean);
  34.     procedure Open1Click(Sender: TObject);
  35.     procedure PlayCD1Click(Sender: TObject);
  36.     procedure Exit1Click(Sender: TObject);
  37.   private
  38.     { Private declarations }
  39.   public
  40.     { Public declarations }
  41.   end;
  42.  
  43. var
  44.   Form1: TForm1;
  45.  
  46. const
  47.   MediaDir = '\Windows\Media'; { change if you want a different default dir }
  48.   FormCaption = 'PC Plus Media Player';
  49.  
  50. implementation
  51.  
  52. {$R *.DFM}
  53.  
  54. procedure TForm1.FormActivate(Sender: TObject);
  55. begin
  56.   ChDir(MediaDir);                      { change to default media directory }
  57.   Form1.Caption := FormCaption;
  58. end;
  59.  
  60.  
  61.  
  62. procedure TForm1.MediaPlayer1Click(Sender: TObject; Button: TMPBtnType;
  63.   var DoDefault: Boolean);
  64. { test the Button parameter to display a descriptive label }
  65. begin
  66.   case Button of
  67.     btPlay : Label1.Caption := 'Playing';
  68.     btPause: Label1.Caption := 'Paused';
  69.     btStop:  Label1.Caption := 'Stopped';
  70.     btNext:  Label1.Caption := 'Next';
  71.     btPrev:  Label1.Caption := 'Previous';
  72.     btEject: Label1.Caption := 'Eject';
  73.   end;
  74. end;
  75.  
  76. procedure TForm1.Open1Click(Sender: TObject);
  77. { Pop up File dialog. Try to open media file selected }
  78. begin
  79.    if OpenDialog1.Execute then
  80.       { if a file is selected then...}
  81.    begin
  82.       if MediaPlayer1.Mode = mpPlaying then
  83.          MediaPlayer1.Stop;
  84.       MediaPlayer1.Close;
  85.       MediaPlayer1.Devicetype := dtAutoselect;
  86.       MediaPlayer1.FileName := OpenDialog1.FileName;
  87.       Form1.Caption := ExtractFileName(OpenDialog1.FileName);
  88.       try
  89.         MediaPlayer1.Open;
  90.       except
  91.         MessageDlg(MediaPlayer1.ErrorMessage, mtWarning, [mbOK], 0);
  92.         Label1.Caption := 'No media loaded!';
  93.       end;
  94.       if MediaPlayer1.Error = 0 then
  95.          Label1.Caption := 'Media loaded...';
  96.    end;
  97. end;
  98.  
  99. procedure TForm1.PlayCD1Click(Sender: TObject);
  100. { Open and play CD }
  101. begin
  102.    if MediaPlayer1.Mode = mpPlaying then
  103.       MediaPlayer1.Stop;
  104.    MediaPlayer1.Close;
  105.    MediaPlayer1.Filename := '';
  106.    MediaPlayer1.Devicetype := dtCDAudio;
  107.    MediaPlayer1.Open;
  108.    MediaPlayer1.Play;
  109.    Label1.Caption := 'Audio CD';
  110.    Form1.Caption := 'Playing CD';
  111. end;
  112.  
  113. procedure TForm1.Exit1Click(Sender: TObject);
  114. begin
  115.   Close;
  116. end;
  117.  
  118. end.
  119.