Friday, December 30, 2016

Media Player - Delphi tutorial (Tip for 10 )





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.

3 comments:

  1. Hmm... very good Kobyx =D

    ReplyDelete
  2. Hello. When I start the video, I get this error. No MCI DEVISE OPEN. How to remove it? Thank you.

    ReplyDelete
  3. One of the brightest participants of the Voice of the Land-12 is visiting us today. This guy turned three judges around. He fascinated them not only with his timbre, but also with the author's song. This is Maxim Borodin — singer, musician and participant of the Voice of the Land project. Maxim joined the cultural front, which was created by the people's coach Andrei Matsola and together with other participants went to perform concerts to the military. He told about this experience and his new song "My Land" in the Snidanka with 1+ 1.https://sound-library.net/

    ReplyDelete

Popular Posts

Recent Posts

Unordered List

Text Widget

Pages

Search This Blog

Powered by Blogger.

Contributors

Text Widget