Reputation: 4624
I have created a windows media player ActiveX control.
(see: Windows Media Player in Delphi for further reference).
Everything works fine. here is sample from my code:
uses ..., WMPLib_TLB;
procedure TForm1.FormCreate(Sender: TObject);
begin
MP := TWindowsMediaPlayer.Create(Self);
MP.Parent := Panel1;
MP.Align := alClient;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
MP.controls.stop;
MP.URL := 'https://www.w3schools.com/html/mov_bbb.mp4';
MP.controls.pause;
MP.controls.currentPosition := 2; // 2 seconds
end;
I want to open a video file/url and show the video frame at a position of X seconds.
However the above code does not stop/pause the video at 2 second and the video simply plays until the end.
How can I pause the frame at position X?
EDIT: just to be clear, I want to get a "preview" effect, so when I load the video for the first time the media player will show some (first) frame instead of a black screen. then the user can press play to watch the video.
Upvotes: 0
Views: 2505
Reputation: 4624
Here is my solution for now.
IMO it is far from being elegant, but it seems to work.
The key point as @Sertac mentioned in the comments is that the media player is loading asynchronous:
or when state changes to be wmppsPlaying. If loading is asynchronous, playing may begin well after the click handler returns.
procedure TForm1.FormCreate(Sender: TObject);
begin
MP := TWindowsMediaPlayer.Create(Self);
MP.Parent := Panel1;
MP.Align := alClient;
Timer1.Enabled := False;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
FFirstTimePlay := True;
Timer1.Interval := 50;
MP.OnPlayStateChange := MP_PlayStateChange;
MP.controls.stop;
MP.settings.autoStart := False;
MP.URL := 'https://www.w3schools.com/html/mov_bbb.mp4';
MP.controls.play; // start playing
end;
procedure TForm1.MP_PlayStateChange(ASender: TObject; NewState: Integer);
begin
Timer1.Enabled := (NewState = wmppsPlaying) and FFirstTimePlay;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
if FFirstTimePlay and MP.controls.isAvailable['currentPosition'] then
begin
FFirstTimePlay := False;
MP.controls.currentPosition := 2;
MP.controls.pause;
end;
end;
This code will go to the position at 2 second and pause.
Now the user can press play to continue playing the rest of the video...
There is a small visual side effect that the video is "jumping" from an initial (zero) location to the X position.
Again, IMO this is far from being elegant, but maybe this is the only way.
Upvotes: 1