Author: h4ry p
Difficulties in moving forward/backward (changing position) the current played
track in TMediaPlayer ??
Answer:
To change the current position of current playing track, you just need to take the
usefull (advance) of two event: 1) onTimer of TTimer and 2) onChange of TScrollbar.
For full code, read below.
Here are the codes:
1 procedure TForm1.Button1Click(Sender: TObject);
2 begin3 if (OpenDialog1.Execute) then4 begin5 Timer1.Enabled := false;
6 MediaPlayer1.FileName := OpenDialog1.FileName;
7 MediaPlayer1.Open;
8 ScrollBar1.Max := MediaPlayer1.Length;
9 ScrollBar1.Position := 0;
10 Timer1.Enabled := true;
11 end;
12 end;
13 14 procedure TForm1.Timer1Timer(Sender: TObject);
15 begin16 ScrollBar1.OnChange := nil; // disable the event handler17 ScrollBar1.Position := MediaPlayer1.Position;
18 ScrollBar1.OnChange := ScrollBar1Change; // enable it again19 end;
20 21 procedure TForm1.ScrollBar1Change(Sender: TObject);
22 begin23 MediaPlayer1.Pause;
24 MediaPlayer1.Position := ScrollBar1.Position;
25 MediaPlayer1.Play;
26 end;
First thing you must do here is initiate the MAX range of TScrollBar with the
length of the current song track (look at Button1Click above).
Then add onTimer event code like above, and so the onChange event of TScrollBar.
The key is you must set TMediaPlayer's position with your selected scroolbar position for each of onTimer happen. Do this by calling onChange event of TScrollBar in onTimer event of TTimer.