Filter löschen
Filter löschen

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

3 Ansichten (letzte 30 Tage)
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 Kommentare
Mario Malic
Mario Malic am 14 Sep. 2020
In your function play, put drawnow maybe with limitrate.
Geoff Hayes
Geoff Hayes am 15 Sep. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by