フィルターのクリア

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

3 ビュー (過去 30 日間)
Lima Kilo
Lima Kilo 2020 年 9 月 14 日
コメント済み: Geoff Hayes 2020 年 9 月 15 日
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 件のコメント
Mario Malic
Mario Malic 2020 年 9 月 14 日
In your function play, put drawnow maybe with limitrate.
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 Center および File ExchangeGet Started with MATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by