%-----------------------------   PLAYER        ---------------------------------
% small video player demo to show how to handle timer and waitbar together
%
% usage  :   player(video_file)
%
% input  :   video_file (char) -- path to video file
%
%
% example:   player('xylophone.mpg')
% 
%-------------------------------------------------------------------------------
function player(video_file)
if nargin==0
   disp('player need some movie file')
   return;
end



if exist(video_file,'file')
   
   m_ver          = ver('matlab');
   m_ver.Version  = regexprep(m_ver.Version,'\.','');
   m_ver          = sscanf(m_ver.Version,'%f');
   
   if  m_ver < 711
      vObj        = mmreader(video_file);
   else
      vObj        = VideoReader(video_file);
   end
   
   daten.fps      = round(vObj.FrameRate);
   daten.vidlen   = vObj.NumberOfFrames;
   
   w = vObj.Width;
   h = vObj.Height;
   
   [handles, fig] = create_gui(mfilename(),w,h);
   handles.daten  = daten;
   handles.vObj   = vObj;   
   guidata( fig, handles );
   
   handles      = init_timer(handles);
   guidata( fig, handles );
   
   set(handles.hPlayerTimer,'Period'  , floor(1/daten.fps*1000)/1000)
   start(handles.hPlayerTimer);
else
   return;
end

%-------------------------------------------------------------------------------
%
%-------------------------------------------------------------------------------
function [handles,fig] = create_gui(fig_name,w,h)

search_name     = regexprep(fig_name,'\.(.)+','\.fig');
fig             = findall(0,'Type','figure','-regexp','Tag',search_name);
if isempty(fig)
   fig               = figure(           ...
      'Position'  ,  [300 300 w h] ,...
      'Tag'       ,  search_name       ,...
      'MenuBar'   ,  'none'            ,...
      'Toolbar'   ,  'none'            ,...
      'CloseRequestFcn',@close_gui);
   handles           = guihandles(fig);
   handles.hfig      = fig;
   handles.hax_video = axes('Parent',fig, 'Units','pixel');
else
   handles      = guidata(fig);
   pos          = get(fig,'Position');
   set(fig,'Position',[pos(1),pos(2),w,h]);
end
set(handles.hax_video,'Position',[0,0,w,h]);
handles         = init_settings(handles,w,h);


%-------------------------------------------------------------------------------
%
%-------------------------------------------------------------------------------
function close_gui(src,event)
handles = guidata(src);
stop(handles.hPlayerTimer  );
delete(handles.hPlayerTimer);
pause(0.5);
delete(src)

%-------------------------------------------------------------------------------
%
%-------------------------------------------------------------------------------
function handles = init_settings(handles,w,h)

handles.him_video    = image(zeros(h,w,3,'uint8'),'Parent',handles.hax_video);
set(handles.hax_video,    'XTick', []);
set(handles.hax_video,    'YTick', []);
axis image;

%-------------------------------------------------------------------------------
%
%-------------------------------------------------------------------------------
function handles = init_timer(handles)
hPlayerTimer    = timer(                          ...
   'ExecutionMode'  , 'fixedRate'              ,...
   'Name'           , 'PlayerTimer'           ,...
   'StartFcn'       , {@PlayerTimer,guidata(handles.hfig)}  ,...
   'StopFcn'        , {@PlayerTimer,guidata(handles.hfig)}  ,...
   'TimerFcn'       , {@PlayerTimer,guidata(handles.hfig)}  );
handles.hPlayerTimer = hPlayerTimer;

%-------------------------------------------------------------------------------
%
%-------------------------------------------------------------------------------
function PlayerTimer(timerObj,event,handles)

switch(event.Type)
   case 'StartFcn'
      control.run          = true;
      control.stimer       = event.Data.time;
      control.counter      = 1;
      control.waitbar      = waitbar(0,'player started');
      set(timerObj,'UserData', control); 
%       ;
   case 'StopFcn'
      control              = get(timerObj,'UserData');
      control.run          = false;
      set(timerObj,'UserData',control);
      close(control.waitbar);      
   case 'TimerFcn'
      control              = get(timerObj,'UserData');
      counter              = control.counter;
      if counter > handles.daten.vidlen
         stop(timerObj);
         return;         
      end
      waitbar(counter/handles.daten.vidlen,  control.waitbar);
      
      if control.run 
         Frame             = read(handles.vObj,counter);
         control.counter   = counter + 1;
         set(timerObj,'UserData',control);
         if ishandle(handles.him_video)
            set(handles.him_video,'CData',Frame);
         end
      end
end



