Stopping showing a MP4 file (frame) by exit button

2 views (last 30 days)
Hello,I am reading a mp4 video and have created a figure with the axes, and two buttons. My two buttons for play and exit have the following callbacks associated to them:
function exitCallback(app, source, eventargs,videoSrc,hFig)
% Close the video file
%release(videoSrc); This doesn't work, so try using delete
delete(videoSrc);
% Close the figure window
close(hFig);
end
function playCallback(app, source, eventargs,v,hAxis)
try
dur=v.Duration;
axes(hAxis);
while(hasFrame(v))
frame = readFrame(v);
imshow(frame); %colormap(jet)
title(sprintf('Current Time = %.3f sec (%.3f)', v.CurrentTime,dur),'Color', 'r');
pause(2/v.FrameRate);
end
catch
end
end
The playback works find and also the exitcallback sort of works. It does stop the video and delete the videoSrc (v) but it then causes the while loop in the playcall back to throw an error. Is there a way so stop the playing without causing an error. I was looking for a stopreadingframe(v) sort of function, but it doesn't exist.
For info, here's the main part of my code.
% Get File
try
[file,folder]=uigetfile({'*.mp4','MP4 Files'},'Open video',app.VideoSaveDirEditField.Value);
catch
[file,folder]=uigetfile({'*.mp4','MP4 Files'},'Open video','C:\');
end
filename=fullfile(folder,file);
v = VideoReader(filename);
%Get info from video
D=['Duration(s): ',num2str(v.Duration,'%.2f')];
B=['BitsperPixel: ',num2str(v.BitsPerPixel)];
F=['FrameRate: ',num2str(v.FrameRate)];
N=['Num Frames: ',num2str(v.NumFrames)];
mstr=({D;F;B;N}); % Use braces
f = msgbox(mstr,'icon','help'); % strcat is not necessary. The straight and complete answer is the use of {} instead of [ ] to have the messaeg in the msgbox.
waitforbuttonpress;
%v.Colormap = jet
ReportMessage(app,['Opened Video ',file]);
numFrames=v.NumFrames;
ReportMessage(app,['...Num Frames: ',num2str(numFrames)]);
fRate=v.FrameRate;
ReportMessage(app,['...FrameRate: ',num2str(fRate)]);
dur=v.Duration;
f1=figure('Name', 'MP4 Preview Window', 'Units','normalized',...
'Position',[0.3 0.4 0.5 0.5]);
% uicontrol('String', 'Close', 'Callback', {@app.exitCallback,v,f1}); % uicontrol('String', 'Close', 'Callback', 'close(gcf)');
pos=[0 0 1 1];
hPanel = uipanel('parent',f1,'Position',pos,'Units','Normalized');
% Create axis
hAxis = axes('position',[0.1 0.1 0.8 0.85],'Parent',hPanel);
hAxis.XTick = []; hAxis.YTick = [];
hAxis.XColor = [1 1 1]; hAxis.YColor = [1 1 1];
% Play button with text Start/Pause/Continue
uicontrol(f1,'Units', 'pixels','Style','pushbutton','String','Start',...
'Position',[20 10 100 40], 'Callback', {@app.playCallback,v,hAxis});
% Play button with text STOP
uicontrol(f1,'Units', 'pixels','Style','pushbutton','String','Stop',...
'Position',[130 10 100 40], 'Callback', {@app.exitCallback,v,f1});
%display first frame
frame1 = readFrame(v);
axes(hAxis);
imshow(frame1);
Thanks for any help
Jason

Accepted Answer

Image Analyst
Image Analyst on 30 Jan 2023
You could make a checkbox on the window called chkFinishNow and then check it in your loop
app.chkFinishNow.Value = false;
while(hasFrame(v)) & ~app.chkFinishNow.Value % Continues as long as the checkbox is not checked.
frame = readFrame(v);
imshow(frame); %colormap(jet)
title(sprintf('Current Time = %.3f sec (%.3f)', v.CurrentTime,dur),'Color', 'r');
pause(2/v.FrameRate);
end
% Make sure it's turned off again.
app.chkFinishNow.Value = false;
  6 Comments
Jason
Jason on 30 Jan 2023
Thanks I.A, ill give that suggestion a go. Although without the drawnow, after pressing the checkbox, the loop carried on many many times, not just once. The FrameRate is about 20 fps, so the pause is 2/20 s, so small.
Image Analyst
Image Analyst on 30 Jan 2023
Maybe it's just in such an intensive loop that it doesn't get around to checking the checkbox for a while. I think though that a drawnow or pause would let it stop for air enough to go and check other OS messages (like the box was checked). If the timing is that critical you may have to look for other ways to break out of the loop.

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!