delphiaudiomedia-playerrepeat

How to repeat sounds using Tmediaplayer


i am using delphi7. I want put a song in my program, but i don't want it to end never. I tried using a timer, but it didn't play the music:

procedure TForm1.FormCreate(Sender: TObject);
begin
timer1.enabled:=true;
end;


procedure TForm1.Timer1Timer(Sender: TObject);
var playsound,destination:string;
begin

destination:=paramstr(0); 
playsound:=extractfilepath(destination)+'Soundtrack\play.wma';

mediaplayer1.FileName:=playsound;                
mediaplayer1.Open;
mediaplayer1.Play;                        //USING TMEDIAPLAYER

end;

There are no syntax errors in this code, however the song is not running, perhaps the timer is not for that job. How should i do it? Thanks


Solution

  • Don't use a timer for this. Use the TMediaPlayer.OnNotify event instead:

    procedure TForm1.FormCreate(Sender: TObject);
    begin
      mediaplayer1.FileName := extractfilepath(paramstr(0))+'Soundtrack\play.wma';
      mediaplayer1.Notify := true;
      mediaplayer1.Wait := false;          
      mediaplayer1.Open;
    end;
    
    procedure TForm1.MediaPlayer1Notify(Sender: TObject);
    begin
      case mediaplayer1.Mode of
        mpOpen, mpStopped: begin
          if mediaplayer1.Error = 0 then begin
            mediaplayer1.Notify := true;
            mediaplayer1.Wait := false;
            mediaplayer1.Play;
          end;
        end;
      end;
    end;