Regarding variables that can be accessed across callback functions in App designer

37 views (last 30 days)
I am implenting an audio player in an app. I have two buttons - Play/pause and stop. I want to store the state of the player in a variable called status. Initially, the status is 'stopped'. If Play/pause is pressed now, then the status holds 'playing' after initiating an audio player handle for the media and playing it. If the player is already playing and 'Play/pause' is pressed, the player pauses and the status is set to 'paused'. If the player is paused and the Play/pause button is pressed, then as expected the status is set to 'playing' and the player resumes.
Therefore I need this status variable to be accessible by both the play/pause button and stop button callbacks. Additionally, I think the player handle should be global (in the sense described above) as the music shouldnt start over agin from the beginning if the Play/pause button is pressed when the music is paused. I am entirely new to object oriented programming, so kindly bear with me if I follow up your answer with an even more basic question.
Thank you for any guidance in advance.
  1 Comment
Balakrishnan Rajan
Balakrishnan Rajan on 30 Jan 2019
This is the relevant section of the present code which isnt working:
% Code that executes after component creation
function startupFcn(app)
status = 'Stopped';
end
% Button pushed function: PlayPauseButton
function PlayPauseButtonPushed(app, event)
if(status == 'Stopped')
[y, Fs] = audioread(app.FileLocationEditField.Value);
player = audioplayer(y, Fs);
play(player);
status = 'Playing';
elseif(status == 'Paused')
resume(player);
status = 'Playing';
elseif(status == 'Playing')
pause(player);
status = 'Paused';
end
end
% Button pushed function: STOPButton
function STOPButtonPushed(app, event)
stop(player);
status = 'Stopped';
end

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 9 Feb 2019
Add a property to you app called status. This creates a field in the app structure called status. See this page for an example.
When referring to the variable in your code, refer to it as app.status. You'll notice the structure "app" is the first input in all your callback functions, so this automatically passes the status between all callback functions.
properties (Access = private)
status % player status
end
...
% Code that executes after component creation
function startupFcn(app)
app.status = 'Stopped';
end
% Button pushed function: PlayPauseButton
function PlayPauseButtonPushed(app, event)
if(app.status == 'Stopped')
[y, Fs] = audioread(app.FileLocationEditField.Value);
player = audioplayer(y, Fs);
play(player);
app.status = 'Playing';
elseif(status == 'Paused')
resume(player);
app.status = 'Playing';
elseif(status == 'Playing')
pause(player);
app.status = 'Paused';
end
end
% Button pushed function: STOPButton
function STOPButtonPushed(app, event)
stop(player);
app.status = 'Stopped';
end

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!