Solved

Mediaplayer1.Play; how to loop?

Posted on 1997-12-25
9
315 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now