Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TTrackBar change TMediaPlayer Position 1

Status
Not open for further replies.

ug505

Programmer
Jan 2, 2010
52
US
Hello,

I'm trying to put a trackbar onto my mediaplayer. The trackbar works fine for just tracking the position but I don't know how to make it change the mediaplayer position when it is dragged. Here's the code I have so far:

Code:
procedure TForm1.Timer1Timer(Sender: TObject);
begin
TrackBar1.Max:=MediaPlayer1.Length;
TrackBar1.Position := MediaPlayer1.Position;
end;

Does anyone know how to change the TMediaPlayer position when you drag the trackbar?
 
thread102-1459740

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Glenn - That was not only the longest thread in Tek-Tips history but one of the most interesting and educational ever. Although I did my best to follow it back in 2008, my workload at the time was prohibitive. Now that I have some time (and that this post reminded me) I've decided to review it and play with it. In your post of 5-Apr-08 10:45 you left a link to a zip file:
... that now appears to be broken (Bing'ed?)

So the question is: do you have a replacement download link?

Thanks in advance!

Roo
Delphi Rules!
 
Glen9999, that code you have is too advanced for me. Is there a easier way?
 
So the question is: do you have a replacement download link?

Mediaplayer Demos.zip

Glen9999, that code you have is too advanced for me. Is there a easier way?

The code isn't all about the TTrackBar. But the biggest part of it is that to keep the TTrackBar in sync with the TMediaPlayer you have to update it (semi) continuously, and that was part of the issue of the thread. Then to be able to use the TTrackBar to change the TMediaplayer position, you have to be able to accurately know the position in the file to begin with for the TTrackBar to make any logical sense.

So for that part, no, there really isn't an easier way. Try to study and see how the code is both updating the TTrackBar with the TMediaPlayer position and how it allows the TTrackBar to update the TMediaPlayer position if it is changed.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
@Glenn (Sky Eagle ;-)) - BIG Thanks!!!
You put it right where I hoped you would!

@ug - That code is no doubt over most of our heads, including mine; which is why I said:
That ... thread [is] one of the most ... educational ever.
So I challenge you to seize the opportunity. The short easy answers are soon forgotten!

Roo
Delphi Rules!
 
You put it right where I hoped you would!

It's just a junk account I use when I think my "business account" is going to get spammed. I had a lot of other stuff on there, but the way Skydrive does things is pretty "abrasive" to me, but I really haven't found any other options.

You may have noticed if you ended up browsing it a little that I updated the controls linked to in faq102-7233 and faq102-7231 , fixing a couple of things, and adding one feature. I don't know if anyone else besides me is actually using them, but are (again) some good learning opportunities if anything else (pointing to the next quote).

That code is no doubt over most of our heads, including mine;

Actually if you don't encounter something that is "over your head" every once in a while, you're not learning anything new. In fact, most of what I end up posting (FAQ wise anyway) are things I learn in trying to do something that seem out of the ordinary different, or are the fruit of something I saw that was "over my head". Same really goes with anyone here, I'm sure there are others who can post stuff that is over my head or most anyones.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
I have a TTimer that updates the trackbar, I'm just having trouble moving the position when you move the trackbar. I want it to pause the mediaplayer onclick or ondrag then when you let go it unpauses it. I tried OnStartDrag but that didn't work. Why wouldn't this work?

Code:
procedure TForm1.TrackBar1StartDrag(Sender: TObject;
  var DragObject: TDragObject);
begin
MediaPlayer1.Pause;
MediaPlayer1.Position := TrackBar1.Position;
end;
 
I tried OnStartDrag but that didn't work. Why wouldn't this work?

Because that's for handling drag and drop operations. Not changing the TrackBar.

And why you are probably having problems is because you aren't doing things right to be able to have things work. The Timer is fine as long as you just want to track the data, but it becomes very difficult to impossible to make the Trackbar work while playing without using Threads. This is because you have to disable the trackbar every time you update it with the Mediaplayer value, so effectively it becomes disabled while playing the file.

How to do this task properly and completely is in the file that was posted (Demo #2). Why not just study and learn how to do it instead of just rejecting it outright?

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
I did realize that I had the Trackbar disabled in the first one for some reason...removing the line that does that should get it to work like the first one.

Anyhow, here's the relevant parts.

Code:
procedure TForm1.TrackBar1Change(Sender: TObject);
begin
  MediaPlayer1.Position := TrackBar1.Position;
  Mediaplayer1.Play;
end;

Code:
procedure TForm1.Timer1Timer(Sender: TObject);
begin
  TrackBar1.OnChange := nil;
  Trackbar1.Position := MediaPlayer1.Position;
  TrackBar1.OnChange := TrackBar1Change;
  Label1.Caption := format_str(MediaPlayer1.Position) + track_str;
  if MediaPlayer1.Position >= LyricRecord[LyricPos].Offset then
    begin
      Edit1.Text := LyricRecord[LyricPos].LyricLine;
      inc(LyricPos);
    end;
  if MediaPlayer1.Position = MediaPlayer1.Length then
    begin
      Button2.Enabled := true;
      Button3.Enabled := false;
      Timer1.Enabled := false;
      LyricPos := 1;
    end;
end;

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top