Delphi內建的mediaplayer似乎無法播放MP4的影片!只好透過系統內的mediaplayer來實作了~
一、Component→Import Component然後選擇ActiveX Control
二、搜尋media,找到Windows Meida Player
三、原則上就是下一步安裝完成就好
接下來程式的部份
在畫面上放上WindowsMediaPlayer1、Button、Timer、OpenDialog、ProgressBar1。
程式碼如下:
一、
procedure TForm1.FormCreate(Sender: TObject);
begin
WindowsMediaplayer1.uiMode := ‘none’; //不顯示按鈕
end;
二、
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
WindowsMediaPlayer1.controls.stop;
end;
三、
procedure TForm1.Button1Click(Sender: TObject);
begin
if opendialog1.Execute then begin
WindowsMediaPlayer1.URL := ‘file://’+opendialog1.FileName;
WindowsMediaplayer1.uiMode := ‘none’;
WindowsMediaPlayer1.controls.play;
end;
end;
四、
procedure TForm1.WindowsMediaPlayer1PlayStateChange(ASender: TObject;
NewState: Integer);
begin
case NewState of
wmppsPlaying: begin
Timer1.Enabled := True;
end;
wmppsStopped,
wmppsPaused: begin
Timer1.Enabled := False;
end;
end;
end;
五、
procedure TForm1.Timer1Timer(Sender: TObject);
var
Duration: double;
Position: double;
begin
Form1.Caption := WindowsMediaPlayer1.controls.currentPositionString
+ ‘ of ‘
+ WindowsMediaPlayer1.currentMedia.durationString;
Duration := WindowsMediaPlayer1.currentMedia.duration;
Position := WindowsMediaPlayer1.controls.currentPosition;
ProgressBar1.Position := Trunc( Position*(Duration/ProgressBar1.Max) );
end;