Audioplayer StopFcn is executed even when I pause my audioplayer

7 views (last 30 days)
Shadow_man
Shadow_man on 5 Apr 2022
Commented: Shadow_man on 7 Apr 2022
Hey everyone,
I should start by saying that I am fairly new to MATLAB.
I am using the audioplayer object in my matlab application to play an audio track. I want to perform certain actions when the audio track comes to an end. I did some searching and found out that I can make use of Audioplayer's StopFcn and use a callback function along with it.
Here is my code:
app.SoundPlayer.StopFcn = createCallbackFcn(app, @PlayerStoppedFcn, false);
function PlayerStoppedFcn(app)
disp("player stopped or paused")
end
Within my application I have start, stop, pause and resume buttons. I had the assumption that the StopFcn would only execute when the track ends or stops. However, even when I pause my audioplayer using the pause function, my callback function is called.
I wanted to know if there is a way around this? I only want my actions to take place at the very end of the track and not when it is paused, etc.
Thanks in advance!

Accepted Answer

Geoff Hayes
Geoff Hayes on 5 Apr 2022
@Shadow_man - it doesn't seem correct that the stop function is called when the audio player is paused especially given the documentation here indicates that Function to execute at the end of playback, specified as a character vector or string scalar containing the name of the function, or a function handle. However, calling pause does (for me) call the stop function callback (as it does if I call stop or playback ends).
One thing that I noticed is that when the audio player is paused, the CurrentSample property isn't 1 (assuming that the audio player has been playing some audio) whereas the CurrentSample is 1 if the audio track has completed OR if I call stop. You could perhaps check the sample count and use that as an indicator of whether the track is paused or stopped (though there may be an edge case where the user presses pause immediately after play and I suppose the the sample count could be one).
An alternative might be to remove the stop function callback prior to calling pause and then restoring it prior to calling resume. This would mean that the callback wouldn't be called when paused. If you do this, you might need to add a check in the stop button callback to ensure that the stop function is called (if user calls pause then stop and without restoring the stop function callback to the player).
  1 Comment
Shadow_man
Shadow_man on 7 Apr 2022
Thank you so much, checking whether CurrentSample is equal to 1 or not was satisfactory for my application!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!