App Designer: Button callback function to stop loop reacts only with delay

7 visualizzazioni (ultimi 30 giorni)
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 Commenti
Geoff Hayes
Geoff Hayes il 15 Set 2020
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.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by