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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TThreads + MediaPlayer + Interupting = Hair Loss

Status
Not open for further replies.

EricDraven

Programmer
Jan 17, 2002
2,999
GB
Hi all, bit of a problem!

Im trying to make a program which is basically a slide show of pictures accompanyed by music related to the pictures by the year they were taken. Ive utilised the mediaplayer component to do this but once started, the procedure I have written runs right through the music and photos (as it should) without the user being able to quit the procedure. Because of this I decided a thread was the right way to go which means that the user can exit at any time right! Wrong, The thread termination only seems to work when the current sing has finished playing which is bearable but delphi is producing a win32 error when closing down the program. Using the debugger I have narrowed this down to the Thread but this is the first time Ive used one so Im not entirely sure about the correct way they should be used. Any help, advice, pointers, hair replacement formulas would be greatly apprecated!

Cheers Eric
 
Could you post some code/pseudo-code to illustrate how this works currently?
 
The main bulk of the code is calling the thead, the actual code in the thread, and the termination of the thread. The picture changes are handled by a timer and run completley seperate to the following code.

1). Calling the thread.

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
MediaPlay := MediaTest.Create(False);
end;

2). Terminating the thread
procedure TForm1.SpeedButton2Click(Sender: TObject);
var
ExitCode : Cardinal;
begin
Edit1.OnChange := nil;
Edit1.Text := '';
Edit1.OnChange := Edit1Change;
GetExitCodeThread(MediaPlay.Handle, ExitCode);
TerminateThread(MediaPlay.Handle, ExitCode);
end;

3). The thread code.
procedure MediaTest.Execute;
var
X, Stop : Integer;
begin
if Form1.Listbox1.Items.Count > 0 then
begin
if Form1.Listbox1.ItemIndex = -1 then
X := 0 else X := Form1.Listbox1.ItemIndex;
Stop := Form1.Listbox1.Items.Count;
repeat
with Form1.MediaPlayer1 do
begin
FileName := Form1.Label1.Caption + '\Music\' + Form1.Listbox1.Items[X];
Form1.Edit1.Text := Form1.Listbox1.Items[X];
Open;
Display := Form1.Panel1;
try
Wait := True;
Play;
finally
Close;
end;
end;
X := X + 1;
until X = Stop;
end;
end;

Listbox1 contains a list of all available tracks and label1 contains the current path because for some reason, you cannot use extractfilepath in a thread.
 
You shouldn't really use TerminateThread. Instead kill it by calling MediaTest.Terminate (I'm assuming you meant MediaTest and not MediaPlay?) and change
until X = Stop;
to
until (X = Stop) or (Terminated);

Although that will still only terminate once the current song has stopped.

I wouldn't bother using a thread if I were you. Set Wait to false and use the TMediaPlayer.Notify event to trigger the loading/playing of the next song once the current one stops.
 
If you decide to stay with the threading, don't forget to check the Terminated flag in your loop, exited when it's true. The .Terminate method only sets this flag to true.

lou
 
Mike, I have tried your suggestion with varying degrees of success but unfortunatley the photos are not updating correctly unless I use a thread (in fact they dont seem to update at all once the music starts playing). A new photo related to the year is to be displayed evry 10 seconds or so. Dont suppose anybody can suggest a way of doing this?
 
When I set the wait value to false the mediaplayer just seems to fly through everything in one go even when I use the OnNotify event. However, if I leave the wait property set to True everything seems to work barring the pictures and being unable to use the other buttons on the mediaplayer.
 
If you set Wait to False control will be returned to your application as soon as the sound starts playing, so it will just run through everything without stopping.
You should put your code to change the MediaPlayer filename in the OnNotify event, and change the filename when this event is triggered and the MPlayer's Mode is mpStopped.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top