You can achieve interrupt playback by setting the Enable
property of your slider to off
or inactive
when you press the play button, and using the ButtonDownFcn
function, which stops playing and sets Enable
to on
.
Using togglebutton
as my play button (other control widgets should work as long as you can keep the boolean flag somewhere accessible), I used the following as a Callback
for the button:
function playcallback(toggle_button, ~, slider_) set(slider_, 'Enable', 'inactive'); %slider is disabled while get(toggle_button, 'Value') %Value is used as flag for playing current_value = get(slider_, 'Value'); set(slider_, 'Value', rem(current_value + 0.01, 1)); %shift slider (looping) pause(1/50); end set(slider_, 'Enable', 'on'); %done playing, turn slider back on end
And for the slider: ButtonDownFcn
:
function stopslide(~, ~, toggle_button) %play flag off: in playcallback, the while loop stops, %the slider is enabled and the playcallback function returns set(toggle_button, 'Value', 0); end
You can register these callbacks as follows:
set(toggle_button_handle, 'Callback', {@playcallback, slider_handle}); set(slider_handle, 'ButtonDownFcn', {@stopslide, toggle_button_handle});
Caution: if you start adding other widgets that interact with the slider / play button, similarly, you will increase the likelihood of race conditions appearing.