Stopping showing a MP4 file (frame) by exit button

조회 수: 2 (최근 30일)
Jason
Jason 2023년 1월 30일
댓글: Image Analyst 2023년 1월 30일
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

채택된 답변

Image Analyst
Image Analyst 2023년 1월 30일
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
Jason
Jason 2023년 1월 30일
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 2023년 1월 30일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Audio and Video Data에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by