Media Player - Delphi tutorial





Delphi example showing how to create Media Player.
Here you can see how to create Media Player with which you can play music and video files.
It is quite simple and easy way to do this with Delphi.



Following is the source code of this example :



unit MainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, MPlayer, ExtCtrls;

type
  TfrmMain = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    Label1: TLabel;
    MediaPlayer1: TMediaPlayer;
    ScrollBar1: TScrollBar;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Panel2Resize(Sender: TObject);
    procedure ScrollBar1Scroll(Sender: TObject; ScrollCode: TScrollCode;
      var ScrollPos: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

procedure TfrmMain.Button1Click(Sender: TObject);
begin
OpenDialog1.InitialDir:=ExtractFilePath(Application.ExeName);
if OpenDialog1.Execute then
begin

Timer1.Enabled:=False;
 MediaPlayer1.FileName:=OpenDialog1.FileName;
 MediaPlayer1.Open;
 MediaPlayer1.Display:=Panel2;
 MediaPlayer1.DisplayRect:=Panel2.ClientRect;
 ScrollBar1.Max:=MediaPlayer1.TrackLength[1];
 Timer1.Enabled:=True;
end;
end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
Label1.Parent:=ScrollBar1;
Label1.Left:=Round(ScrollBar1.Width/2);
Label1.Top:=0;
end;

procedure TfrmMain.Panel2Resize(Sender: TObject);
begin
MediaPlayer1.DisplayRect:=Panel2.ClientRect;
Label1.Left:=Round(ScrollBar1.Width/2);
Label1.Top:=0;
end;

procedure TfrmMain.ScrollBar1Scroll(Sender: TObject; ScrollCode: TScrollCode;
  var ScrollPos: Integer);
begin
MediaPlayer1.Position:=ScrollBar1.Position;
MediaPlayer1.Play;
end;

procedure TfrmMain.Timer1Timer(Sender: TObject);
begin
ScrollBar1.Position:=MediaPlayer1.Position;
Label1.Caption:=FormatFloat('0#.##',(ScrollBar1.Position/ScrollBar1.Max)*100)+' %';
if MediaPlayer1.Position=ScrollBar1.Max then MediaPlayer1.Stop;

end;

end.
Share on Google Plus

About Clear SoftX

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.