App Designer: Button callback function to stop loop reacts only with delay
조회 수: 3 (최근 30일)
이전 댓글 표시
I'm trying to build a small video player in App designer, with a play / stop button and a UIAxes to display the frames. Once play is clicked, a while loop runs that displays the video frames until the bool_play flag turns false. The callback function of the play / stop button should set this flag. Pressing the stop button will stop the playback, but only with a delay that seems to grow the longer the loop has been running.
There is a pause() in the loop, so it should be interruptible and the callback should be able to execute.
These are the relevant functions:
function PlayButtonPushed(app, event)
if app.bool_play == false % play video
app.PlayButton.Text = "Stop";
app.bool_play = true;
app.play();
else % stop video
disp('stop clicked');
app.bool_play = false;
app.PlayButton.Text = "Play";
end
end
function results = play(app)
tic
while app.bool_play == true
playbackSpeed = 10; % fps
if app.currentFrameNum >= size(app.vid,3)
app.currentFrameNum = 1; % start over
end
app.display_image();
pauseTime = 1/playbackSpeed;
timeElapsed = toc;
restTime = pauseTime - timeElapsed;
disp([pauseTime timeElapsed restTime]);
if restTime > 0 && app.bool_play == true
pause(restTime);
end
tic
app.currentFrameNum = app.currentFrameNum + 1;
end
end
function results = display_image(app)
frameNum = app.currentFrameNum;
im = app.vid(:,:,frameNum);
app.imageHandle = imagesc(app.UIAxes, im);
app.UIAxes.Title.String = sprintf("Frame %i", frameNum);
app.currentFrameNum = frameNum;
end
What is the reason for this delay and can it be fixed? Or is there maybe a better approach?
The app file is attached.
Thanks!
댓글 수: 2
Geoff Hayes
2020년 9월 15일
Lukas - or perhaps use a timer that fires every 1/10 of a second (1/playbackSpeed) to display the frame. You then should be able to stop the timer when the stop button is pressed.
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!