Recent

Author Topic: Mplayer stop,pause command  (Read 8851 times)

hamza

  • Jr. Member
  • **
  • Posts: 52
Mplayer stop,pause command
« on: April 03, 2015, 08:44:34 pm »
hi all

I found an example how to play vedio or audio file using mplayer on linux.
here in the link
http://wiki.freepascal.org/Multimedia_Programming

and the code is:
uses
  Classes, ..., FileUtil, LCLProc, UTF8Process;
 
...
 
implementation
 
procedure TMainForm.Button1Click(Sender: TObject);
var
  Player: TProcessUTF8;
  Filename: String;
  PlayerPath: String;
  PlayerParams: String;
begin
  Filename:='/home/username/video.mpg';
  PlayerPath:=FindDefaultExecutablePath('mplayer');
  PlayerParams:='"'+Filename+'"';
  Player:=TProcessUTF8.Create(nil);
  try
    Player.CommandLine:=PlayerPath+' '+PlayerParams;
    Player.Execute;
  finally
    Player.Free;
  end;
end;

and I want to add some buttons for stop,puase ...etc . but I want to know how to write the stop and pause  commands using pascal lazarus.


Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Mplayer stop,pause command
« Reply #1 on: April 03, 2015, 09:24:46 pm »
Under Windows and Linux you can use:
http://wiki.lazarus.freepascal.org/TMPlayerControl

As you've found, you don't need to use the control - but have a peek at the source code - all your stop/start/pause answers will be in there.

Or, I guess you could consult the manual :-)
http://www.mplayerhq.hu/DOCS/tech/slave.txt

Ah.  Have considered your question for a while now.  You might not be asking about MPlayer per se, but instead about using TProcess to send and retrieve commands from a running object.

Have a look in MPlayerCtrl.pas - again your answers are in here.  Search for FPlayerProcess, find where it's created, free'd and what events are hooked up to it.  In particular have a look at the TimerEvent...
« Last Edit: April 03, 2015, 10:10:29 pm by Mike.Cornflake »
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

hamza

  • Jr. Member
  • **
  • Posts: 52
Re: Mplayer stop,pause command
« Reply #2 on: April 04, 2015, 12:18:40 am »
Thank u for your reply

But im trying to find some example that explain how to write the comnands using mplayercontrol and without mplayercontrol.

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Mplayer stop,pause command
« Reply #3 on: April 04, 2015, 12:58:06 am »
Thank u for your reply

But im trying to find some example that explain how to write the comnands using mplayercontrol and without mplayercontrol.

With TMplayerControl.  Have a look at the supplied demo. 
  https://svn.code.sf.net/p/lazarus-ccr/svn/components/mplayer/examples/FullFeatured/
  https://svn.code.sf.net/p/lazarus-ccr/svn/components/mplayer/examples/Simple/

From the wiki link I posted...
Quote
In the examples folder, see FullFeatured\mplayerTestHarness.lpr for a full demonstration of TMPlayerControl capabilities, as well as additional functionality for querying mplayer for available commands.

Fullfeatured contains working Start, Stop, Pause.

Honestly, I recommend you just open the Fullfeatured project.  It's got what you said you need, and is exactly the reason why I wrote it.   If you find this project doesn't answer your questions, then please clearly specify exactly what it is you're after and I'll post some sample code. 


...and without mplayercontrol.

However, if you insist on reproducing the functionality of TMplayerControl, then here are some notes:

Without TMplayerControl I would recommend exactly the same workflow that's inside TMplayerControl.  The code is all there, and while sure - you can mock my commenting skills, at least I tried :-)

The issue with the code you posted in the original post is that you free TProcessUTF8.   So, you need to stop to doing that. 
Good example code to how to do this is inside
procedure TCustomMPlayerControl.Play;
procedure TCustomMPlayerControl.Stop;
(basically, this ensures the TProcessUTF8 is alive for the entire duration of the playing video)

Once you've stopped freeing the object, you need a mechanism for sending data to the running instance of mplayer.exe. 
The example code for this is inside:
procedure TCustomMPlayerControl.SetPaused(const AValue: boolean);
which leads you to
SendMPlayerCommand('pause');
(I've bolded that bit because this may be what you're trying to ask)
which leads you to
procedure TCustomMPlayerControl.SendMPlayerCommand(Cmd: string);
which has example code for injecting commands into a running TProcessUTF8, namely
FPlayerProcess.Input.Write(Cmd[1], length(Cmd));

Right, so now we're inputing commands into the running instance of mplayer, we need to start listening to the results the mplayer is giving back to us.  In the TMplayerControl, we're doing this with a timer.  This leads to a little of fun as there is now a disconnect between when we send the request and when we get the information back. 

Finally, this brings us to:
procedure TCustomMPlayerControl.TimerEvent(Sender: TObject);

This contains the code for retrieving the data from the running instance of mplayer.exe, as well as some state management code (if mplayer.exe just told us it's paused the video as we requested earlier, then we better update the paused state flag, that sort of thing).  The code for listening into the TProcessUTF8 is basically
Code: [Select]
  // global variable
  Private
    FOutlist : TStringList;
...
   FOutlist := TStringList.Create;
...
    // and then inside the timer code...
    if FPlayerProcess.Output.NumBytesAvailable > 0 then
    begin
      FOutList.LoadFromStream(FPlayerProcess.Output);

      // bunch of stuff that processes the contents of the stringlist...
    end;

(Disclaimer: Almost none of the above code is mine.  All I did was add the FullFeatured example code and expanded the FOutList.LoadFromStream(FPlayerProcess.Output)  state management processing.  Re-reading this post I make it sound like I wrote it all, and that's simply not the case...)
« Last Edit: April 04, 2015, 01:33:59 am by Mike.Cornflake »
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

hamza

  • Jr. Member
  • **
  • Posts: 52
Re: Mplayer stop,pause command
« Reply #4 on: April 04, 2015, 10:57:50 pm »
thank u very much for your support,, but Im sorry because i am very new on Lazarus and pascal.
also i hope to help me how to write the commands without mplayercontrol

and i just need to add some buttons on the original code in first post to excute some commands like pause, stop...


procedure TMainForm.stopClick(Sender: TObject);
begin
//command to stop mplayer
end;


procedure TMainForm.pauseClick(Sender: TObject);
begin
//command to pasuse mplayer
end;

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Mplayer stop,pause command
« Reply #5 on: April 04, 2015, 11:40:40 pm »
I'm terribly sorry, but using that original code you cannot do what you want to do.  You would need to re-write that code to a large extent.  I've given you all the information you need for that re-write, but it won't be trivial, particularly if you're new to Lazarus/Pascal. 

This doesn't need to be hard though.   Let's try starting again. 

Please forget that original code sample.  I recommend you install the TMPlayerControl package

Quote
svn co https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/components/mplayer mplayer

During the Install Process, you will be asked if you want to restart Lazarus.  Choose yes.  (If you have problems during this phase, let us know.  I admit that installing a package in Lazarus isn't intuitive.)

Once Lazarus has restarted, start a new Application.
On the Component Palette, there is a new Tab called "Multimedia"
Drop a TMPlayerControl on the form.  Resize as you need.  This will be the control that the video plays back through.  Add a Play, Stop and Pause Button...

Add the following code (to get the FormCreate, click on the form in the designer, then find the OnCreate event in the Object Inspector.

Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
Begin
  // Have a go at finding where mplayer is installed
  If Not MPlayerControl1.FindMPlayerPath Then
    MPlayerControl1.MPlayerPath := 'C:\Path\To\MPlayerFolder';  // or '/Path/to/MPlayerFolder';

  {$IFDEF Linux}
  MPlayerControl1.StartParam := '-vo x11';
  {$else $IFDEF Windows}
  MPlayerControl1.StartParam := '-vo direct3d';
  {$ENDIF}
End;

procedure TForm1.PlayClick(Sender: TObject);
begin
  MPlayerControl1.Filename := 'C:\path\To\Video.mp4';
  MPlayerControl1.Play;
end;

procedure TForm1.pauseClick(Sender: TObject);
begin
  MPlayerControl1.Paused := Not MPlayerControl1.Paused;
end;

procedure TForm1.stopClick(Sender: TObject);
begin
  MPlayerControl1.Stop;
end;

Me?  I'm off to delete that code sample in the wiki that started all this...   That code predated the existence of TMplayerControl, I knew I should have removed it last year :-)
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

 

TinyPortal © 2005-2018