How to pause and resume my video which is being displayed on a particular axis in MATLAB app frame by frame.

19 views (last 30 days)
Right now I'm using this code on the Pause button in the MATLAB app.
if strcmp(app.PauseButton.Text,'Pause')
app.PauseButton.Text='Resume';
pause
elseif strcmp(app.PauseButton.Text,'Resume')
app.PauseButton.Text='Pause';
pause('off')
end

Answers (1)

Sameer
Sameer on 28 Mar 2024
Hi Rishika
From my understanding, you want to implement a feature in your MATLAB App Designer application that allows users to pause and resume video playback on a specific axis, with the ability to control this action using a single toggle button. Your current approach using pause and pause('off') is not achieving the desired frame-by-frame video control. What you need is a method to programmatically control the video playback, allowing it to be paused at any frame and then resumed from that point, using a flag to manage the playback state within a video display loop.
To control the playback of a video in a MATLAB App Designer app, especially to pause and resume it frame by frame, you need a different approach than just using pause and pause('off'). The “pause” function is used to halt the execution of the MATLAB code for a specified amount of time and doesn't directly control the video playback. To achieve frame-by-frame control, you would typically use a loop to display each frame of the video and a flag variable to control the pause/resume functionality.
Here's a conceptual approach to implement pause and resume functionality for video playback in MATLAB App Designer:
  • Initialization: Load the video and initialize variables to control playback.
  • Display Loop: Use a loop to read and display each frame of the video on the app's axis. Inside the loop, check the status of a flag variable to determine whether to pause the video.
  • Pause/Resume Button Callback: Change the flag variable's value when the button is pressed to control the video playback.
Below is an example code snippet demonstrating this approach:
% Assume you have a video file named 'myVideo.mp4'
videoFile = 'myVideo.mp4';
v = VideoReader(videoFile);
% Initialize a flag for pause/resume
isPaused = false;
% Display Loop
while hasFrame(v) % reads and displays each frame of the video.
frame = readFrame(v);
image(frame, 'Parent', app.UIAxes); % Assuming app.UIAxes is the axes where you display the video
drawnow;
% Check if the pause button was pressed
while isPaused
pause(0.1); % Small pause to prevent MATLAB from becoming unresponsive
end
% You might want to include a small pause here to control the playback speed
% pause(1/v.FrameRate); % Uncomment to control playback speed
end
% Callback function for the Pause/Resume Button
function PauseButtonPushed(app)
if strcmp(app.PauseButton.Text, 'Pause')
app.PauseButton.Text = 'Resume';
isPaused = true; % Pause the video
elseif strcmp(app.PauseButton.Text, 'Resume')
app.PauseButton.Text = 'Pause';
isPaused = false; % Resume the video
end
end
I hope this helps!
Sameer

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!