Recent

Author Topic: mplayer + sqlite [SOLVED]  (Read 6312 times)

Marcelo Confortino

  • New member
  • *
  • Posts: 8
mplayer + sqlite [SOLVED]
« on: September 04, 2016, 03:39:35 pm »
Good morning!

I am writing an application that must take a picture of the operator when some event is triggered. So far I have mplayer running and showing a webcam's output on screen (see attached screenshot):

Code: Pascal  [Select][+][-]
  1.    {$IFDEF Linux}
  2.    MPlayerControl1.MPlayerPath:='';
  3.    //MPlayerControl1.StartParam:='-vo x11 -zoom -fs';
  4.    MPlayerControl1.Filename := 'tv:// driver=v4l2:device=/dev/video0:gain=1:width=320:height=200:fps=25';
  5.    MPlayerControl1.Play;
  6.   {$else $IFDEF Windows}
  7.    // Download MPlayer generic for Windows and save under Programm Folder Directory
  8.    // http://sourceforge.net/projects/mplayer-win32/
  9.    //Download MPlayer WIN32-Generic
  10.    MPlayerControl1.MPlayerPath:=extractfilepath(application.exename)+'MPlayer\mplayer.exe' ;
  11.    //MPlayerControl1.StartParam:='-zoom -fs';
  12.    MPlayerControl1.Filename:='tv:// driver=dshow:device=0:gain=1:width=320:height=200:fps=25';
  13.    MPlayerControl1.Play;
  14.   {$endif}      

the code that takes the picture and saves the filename to the database is here:

Code: Pascal  [Select][+][-]
  1.       if MPlayerControl1.Running then
  2.         begin
  3.           MPlayerControl1.GrabImage;
  4.           RegistroDataset.FieldByName('imagen').AsString := MPlayerControl1.LastImageFilename;
  5.         end;    

the problem is that the very first invocation of above code does not save the image filename in the database, later invocations work fine.

Am I missing something?

Thanks for your advice.
« Last Edit: September 06, 2016, 01:52:58 pm by Marcelo Confortino »

LacaK

  • Hero Member
  • *****
  • Posts: 691
Re: mplayer + sqlite
« Reply #1 on: September 05, 2016, 10:51:35 am »
You do not show whole database code  :)
There is missing at least ApllyUpdates and Commit ?

Marcelo Confortino

  • New member
  • *
  • Posts: 8
Re: mplayer + sqlite
« Reply #2 on: September 05, 2016, 06:05:09 pm »
Sorry, LacaK. This is the relevant code:
Code: Pascal  [Select][+][-]
  1.       RegistroDataset.FieldByName('nrodni').AsInteger := StrToInt(Edit1.Text);
  2.       RegistroDataset.FieldByName('fechahora').AsString := DateTimeToStr(Now);
  3.       if MPlayerControl1.Running then
  4.         begin
  5.           MPlayerControl1.GrabImage;
  6.           RegistroDataset.FieldByName('imagen').AsString := MPlayerControl1.LastImageFilename;
  7.         end;
  8.       RegistroDataset.Post;
  9.       RegistroDataset.ApplyUpdates;
  10.  

The other fields in the record are saved fine, as is the full record in all but the first execution of the code.
Thank you.

balazsszekely

  • Guest
Re: mplayer + sqlite
« Reply #3 on: September 05, 2016, 06:48:18 pm »
@Marcelo Confortino
Perhaps MPlayerControl1.GrabImage doesn't return the filename immediately. You should wait a few seconds before saving the data into the database.

Marcelo Confortino

  • New member
  • *
  • Posts: 8
Re: mplayer + sqlite
« Reply #4 on: September 05, 2016, 08:22:10 pm »
Thank you, GetMem, I'll try that. Is there a way to know when the image file is ready?

balazsszekely

  • Guest
Re: mplayer + sqlite
« Reply #5 on: September 05, 2016, 08:48:33 pm »
Apparently LastImageFilename is unique, so this should work:

Code: Pascal  [Select][+][-]
  1. //...
  2.   TForm1 = class(TForm)
  3.   private
  4.     function Wait(const DT: QWord; const OldFileName: String): Boolean;
  5.   public
  6.   end;      
  7. //...
  8.  
  9. function TForm1.Wait(const DT: QWord; const OldFilename: String): Boolean;
  10. var
  11.   TC: QWord;
  12. begin
  13.   Result := False;
  14.   TC := GetTickCount64;
  15.   while (GetTickCount64 < TC + DT) and (not Application.Terminated) do
  16.   begin
  17.     if OldFilename <> MPlayerControl1.LastImageFilename then
  18.     begin
  19.       Result := True;
  20.       Break;
  21.     end;
  22.     Application.ProcessMessages;
  23.   end;
  24. end;
  25.  
  26. procedure TForm1.Button1Click(Sender: TObject);
  27. var
  28.   OldFilename: String;
  29. begin
  30.   OldFilename:= MPlayerControl1.LastImageFilename;
  31.   if MPlayerControl1.Running then
  32.   begin
  33.      MPlayerControl1.GrabImage;
  34.      if Wait(5000, OldFilename) then //wait until the filename changes, but no more then five seconds
  35.      begin
  36.         //do the saving here
  37.      end;
  38.   end;
  39. end;

Marcelo Confortino

  • New member
  • *
  • Posts: 8
Re: mplayer + sqlite
« Reply #6 on: September 05, 2016, 11:23:21 pm »
Wow, that was fast, thank you. Sadly, it doesn't work. I even inserted Wait(5000) between grabimage and lastfilename, same result. Looks like some initialization is missing. Reading the wiki, it seems that TMPlayerControl.GrabImage is not fully dependable. I'll keep working on this.

Marcelo Confortino

  • New member
  • *
  • Posts: 8
Re: mplayer + sqlite [SOLVED]
« Reply #7 on: September 06, 2016, 01:57:32 pm »
Well. sort of. I added an additional GrabImage just after the initialization of TMplayerControl. Later, when I call GrabImage and LastFilename it is not the first invocation anymore, and it works as intended. Than you all.

 

TinyPortal © 2005-2018