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
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 CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by