Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I repeat a song?

I have a TMediaPlayer called MediaPlayer1 I then open a file(a song) I play it. now my problem is that I need the song to repeat until the program stops.

The idea is that the form activates and then repeats the specified song until the form is closed.

MediaPlayer1.Filename := 'filename';

Then it opens it

MediaPlayer1.Open;

Then it plays it

MediaPlayer1.Play;

So now the song is playing but when it ends I want it to play again(repeat) and then again until the form is closed.

I tried what David Heffernan said but it does not work, I think I did something wrong can someone pleas correct me.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, MPlayer, StdCtrls;

type
  TForm1 = class(TForm)
    MediaPlayer1: TMediaPlayer;
    Label1: TLabel;
    procedure FormActivate(Sender: TObject);
    procedure MediaPlayer1Notify(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormActivate(Sender: TObject);
begin
 mediaplayer1.FileName:='E:\it project\mario.mid';
 mediaplayer1.Open;
 mediaplayer1.AutoRewind:=true;
 mediaplayer1.Play;
 mediaplayer1.Notify:=true;
end;

procedure TForm1.MediaPlayer1Notify(Sender: TObject);
begin
  if MediaPlayer1.NotifyValue=nvSuccessful then begin
    MediaPlayer1.Play;  
    MediaPlayer1.Notify := True;
  end;
end;
end.
like image 524
Craig Avatar asked Oct 28 '25 16:10

Craig


1 Answers

You have to organise the auto repeat for yourself.

Create an OnNotify event for the media player. This fires when the song completes.

procedure TForm1.MediaPlayer1Notify(Sender: TObject);
begin
  if MediaPlayer1.NotifyValue=nvSuccessful then begin
    //restart the song
    MediaPlayer1.Play;  
    MediaPlayer1.Notify := True;//ensures we are notified when song completes
  end;
end;

The code that starts the song needs to look like this:

MediaPlayer1.AutoRewind := True;//for obvious reasons
MediaPlayer1.Play;
MediaPlayer1.Notify := True;//ensures we are notified when song completes
like image 62
David Heffernan Avatar answered Oct 31 '25 06:10

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!