Solved

Mediaplayer1.Play; how to loop?

Posted on 1997-12-25
9
326 Views
Last Modified: 2012-05-04
hi,

I need it so that mediaplayer loops the file it is playing indefinite.
anyone have an idea.
thanks
0
Comment
Question by:hellfire052497
  • 4
  • 3
  • 2
9 Comments
 
LVL 3

Accepted Solution

by:
Matvey earned 100 total points
ID: 1354598
It's very easy :

You set the MediaPlaey's Notify property to TRUE (not available in design time), and everytime it stops playing the file the OnNotify event occures - so you can play your file over and over again.

Good luck!
0
 

Author Comment

by:hellfire052497
ID: 1354599
how?

tried

Mediaplayer1.Notify := true;
Mediaplayer1.play;

nothing happened, stopped at one play?

0
 
LVL 4

Expert Comment

by:StevenB
ID: 1354600
 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.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 4

Expert Comment

by:StevenB
ID: 1354601
 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.
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1354602
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
0
 
LVL 4

Expert Comment

by:StevenB
ID: 1354603
 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.
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1354604
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
0
 

Author Comment

by:hellfire052497
ID: 1354605
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.

0
 
LVL 3

Expert Comment

by:Matvey
ID: 1354606
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
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question