Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get TWindowsMediaPlayer to play a new track after completion of the old one?

I'm using TWindowsMediaPlayer and am running into a problem. After the current song completes, I can't get it to load a new song and then play it.

procedure TMainWinForm.WMPlayer1PlayStateChange(Sender: TObject;
          NewState: Integer);
  begin
    if (NewState = wmppsMediaEnded) then
      begin
        WMPlayer1.URL := FileScanner.SelectSong;
        writeln('Play triggered on ', String(WMPlayer1.URL));
        WMPlayer1.controls.Play;  // DOES NOT PLAY THE SONG!
      end;
  end;

This loads the song but requires additional user intervention to actually play it. The only way I do get it to continue is to check for wmppsStopped, but that event occurs twice so I get every odd numbered song in the list.

Any ideas on how to make it work right?

like image 842
Glenn1234 Avatar asked Nov 13 '22 05:11

Glenn1234


1 Answers

I got an answer that seems to work. Since TWindowsMediaPlayer seems to be operating asynchronously, you can't trigger events with methods without allowing the others to go. To that end, my guess is that it was rejecting the play method because the media wasn't loaded properly.

procedure TMainWinForm.WMPlayer1OpenStateChange(Sender: TObject;
  NewState: Integer);
begin
  if NewState = wmposMediaOpen then
    begin
      WMPlayer1.controls.play;
    end;
end;

procedure TMainWinForm.WMPlayer1PlayStateChange(Sender: TObject;
  NewState: Integer);
begin
  if (NewState = wmppsStopped) and (SpecialPlayListMode) then
    begin
      WMPlayer1.URL := FileScanner.SelectSong;
    end;
end;

Though this doesn't explain why I can change the URL while something else is playing and play it without any trouble...

like image 117
Glenn1234 Avatar answered Nov 15 '22 06:11

Glenn1234