Link to home
Start Free TrialLog in
Avatar of hellfire052497
hellfire052497

asked on

Mediaplayer1.Play; how to loop?

hi,

I need it so that mediaplayer loops the file it is playing indefinite.
anyone have an idea.
thanks
ASKER CERTIFIED SOLUTION
Avatar of Matvey
Matvey

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hellfire052497
hellfire052497

ASKER

how?

tried

Mediaplayer1.Notify := true;
Mediaplayer1.play;

nothing happened, stopped at one play?

Avatar of StevenB
 I do this:
 
  Hide the mediaplayer component (and its hideous interface) and put your own set of buttons on the form (there it looks nicer already I expect).
  Set up a boolean called perhaps 'LastClickWasPlay'. Make sure that this boolean is True after a click on the Play Button, but False after any other interaction (because the mediaplayer hurls loads of OnNotify events at you).
  Link all your buttons to the media player methods for play, stop etc.
  Create an event handler for the OnNotify event and put something like this in it:

  MediaPlayer1.Notify := True;
  If LastClickWasPlay then begin
    MediaPlayer1.Play;
    StopButtonClick(self);
  end;

  Basically what Matvey said. You'll probably notice that there is still a brief pause between end and replay of the file. I can't get rid of that, (to my annoyance).

  Good Luck, Steven.
 Ignore the bit that says:

StopButtonClick(self);

  it crept in somehow during the paste from my code. It has nothing to do with my comment at all.

  Steven.
Here is the only code you need, I checked it with .wav and .avi, and it worked...

procedure TForm1.FormCreate(Sender: TObject);
begin
MediaPlayer1.Notify := true;
end;

procedure TForm1.MediaPlayer1Notify(Sender: TObject);
begin
if mediaplayer1.Mode = mpStopped then mediaPlayer1.Play;
end;

I use D3, but this has nothing to do with it, I think.
What kind of files do you play?
Could you send the whole code that makes problems?

By the way, Happy new year!
Matvey
 I've not checked it, but will that not make the file replay when, for example, the stop button is pressed? Doesn't the stop button send a notify event which will trigger the event handler you've set up?
  Happy New Year.

  Steven.
Yes, I think it will. So if hellfire wants that mpl to be visible he shell use what you sugested.

Thanks for the hint,
Matvey
Confusing to say the least.

As asked I provide my code here, haven't had time to try the last suggestions

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  TransBtn, ExtCtrls, EnhImage, oxForm, MPlayer, ComCtrls,
  StdCtrls, Gauges, GIFImage, frminit;

type
  TForm1 = class(TForm)
    EnhImage1: TEnhImage;
    Exit: TTransparentButton;
    OpenFile: TTransparentButton;
    Pause: TTransparentButton;
    Play: TTransparentButton;
    Stop: TTransparentButton;
    oxForm1: ToxForm;
    OpenDialog1: TOpenDialog;
    MediaPlayer1: TMediaPlayer;
    Label1: TLabel;
    Gauge1: TGauge;
    Timer1: TTimer;
    GIFImage1: TGIFImage;
    FormInitialize1: TFormInitialize;
    procedure OpenFileClick(Sender: TObject);
    procedure PlayClick(Sender: TObject);
    procedure PauseClick(Sender: TObject);
    procedure StopClick(Sender: TObject);
    procedure ExitClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);

  private
    FFileName: AnsiString;
    procedure SetFileName(const FileName: AnsiString);
    procedure OpenCode(const CreateFile: AnsiString);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}


procedure Tform1.SetFileName(const FileName: AnsiString);
begin
     FFileName := FileName;
     Caption := Format('%s - %s', [ExtractFileName(FileName), Application.Title]);
     OxForm1.Refresh;
end;

procedure TForm1.OpenFileClick(Sender: TObject);
begin
with mediaplayer1 do begin
if OpenDialog1.Execute then
MediaPlayer1.FileName := OpenDialog1.FileName;
MediaPlayer1.DeviceType := dtAutoSelect;
MediaPlayer1.Open;
FFileName := FileName;
Label1.Caption := Format('%s', [ExtractFileName(FileName)]);
end;
SetFileName(OpenDialog1.FileName);
end;

procedure TForm1.PlayClick(Sender: TObject);
begin
with mediaplayer1 do begin
 gauge1.MaxValue := length;
 gauge1.Progress := 0;
 timer1.enabled := true;
 MediaPlayer1.Play;
 end;
end;

procedure TForm1.PauseClick(Sender: TObject);
begin
MediaPlayer1.Pause;
end;

procedure TForm1.StopClick(Sender: TObject);
begin
MediaPlayer1.Stop;
MediaPlayer1.Rewind;
end;

procedure TForm1.ExitClick(Sender: TObject);
begin
try
 FormInitialize1.SaveToRegistry;
 finally
 FormInitialize1.Free;
 end;
Close;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
parameters: string;
x: integer;
WinDirLen: Integer;
WinDir: PChar;
dir : string;
begin
   GetMem(WinDir, 255);
   WinDirLen := GetSystemDirectory(WinDir,255);
   dir := Windir;
   FreeMem(WinDir);
   GifImage1.FileName:= dir + '\lcarsnew.gif';
 FormInitialize1.LoadFromRegistry;
 parameters := '';
     for x := 1 to paramCount do
     begin
     parameters := parameters + paramStr(x);
     if x < paramCount then parameters := parameters +' ';
     end;
     if Parameters <> '' then OpenCode(Parameters);
end;


procedure Tform1.OpenCode(const CreateFile: AnsiString);
begin
     with mediaplayer1 do begin
     MediaPlayer1.FileName := (CreateFile);
     MediaPlayer1.DeviceType := dtAutoSelect;
     MediaPlayer1.Open;
     SetFileName(CreateFile);
     gauge1.MaxValue := length;
     gauge1.Progress := 0;
     timer1.enabled := true;
     gauge1.MaxValue := length;
     gauge1.Progress := 0;
     timer1.enabled := true;
     FFileName := FileName;
     Label1.Caption := Format('%s', [ExtractFileName(FileName)]);
     MediaPlayer1.Play;
     end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
Gauge1.Progress := Mediaplayer1.Position;
end;

end.

Well I also had no time to try yours, but I want to add smth to my answere, and I quote:
"If your application receives an mci_Notify_Successful message, and if the Mode field of the device is set
      to mci_Mode_Stop, you can be certain that the current file has finished playing. That is why the OnNotify
      method shown above checks for such a condition:

      if (MediaPlayer1.NotifyValue = nvSuccessful) and
            (MediaPlayer1.Mode = mpStopped) then
          Edit1.Text := 'File finished playing';  "

So, set notify to true,
assign a OnNotify handler and add the code above, and instead of  instead of changing text just .PLAY again...

Good luck,
Matvey