How to find when audioplayer is finished playing?

8 views (last 30 days)
Here is the code for a button in app designer. I'm trying to make the button display "play" rather that "resume" when the audio track is finished playing, but app.trimmedAudioPlayer.CurrentSample==app.trimmedAudioPlayer.TotalSamples doesn't work for that. Is there another way to find when the track if finished playing
function PlayButtonPushed(app, event)
if strcmp(app.PlayButton.Text, 'Play')
play(app.trimmedAudioPlayer);
disp(app.trimmedAudioPlayer.CurrentSample)
app.PlayButton.Text = 'Pause';
if app.trimmedAudioPlayer.CurrentSample==app.trimmedAudioPlayer.TotalSamples
stop(app.trimmedAudioPlayer);
app.PlayButton.Text = 'Play';
end
elseif strcmp(app.PlayButton.Text, 'Pause')
pause(app.trimmedAudioPlayer);
app.PlayButton.Text = 'Resume';
elseif strcmp(app.PlayButton.Text, 'Resume')
resume(app.trimmedAudioPlayer);
app.PlayButton.Text = 'Pause';
if app.trimmedAudioPlayer.CurrentSample==app.trimmedAudioPlayer.TotalSamples
stop(app.trimmedAudioPlayer);
app.PlayButton.Text = 'Play';
end
end
end

Answers (1)

Aravind
Aravind on 18 Dec 2024
Hi @Ria,
It seems you are using an "audioplayer" object to play audio and want to update the text of a button in App Designer once the audio finishes playing.
There are a couple of approaches you can take. One option is to use the "isplaying" function of the "audioplayer" object. This function returns true (logical 1) if the audio is currently playing, and false (logical 0) otherwise. You can learn more about the "isplaying" function here: https://www.mathworks.com/help/releases/R2023b/matlab/ref/audioplayer.isplaying.html.
Alternatively, you can utilize the "StopFcn" property of the "audioplayer" object. This is a callback function that triggers when the audio playback is complete. You can write a function that updates the button text and assign this function handle to the "StopFcn" property. This ensures the button text changes whenever the audio finishes playing. For more details on the "StopFcn" property, visit: https://www.mathworks.com/help/releases/R2023b/matlab/ref/audioplayer.html#mw_ce25e74d-6a00-44d6-9ae9-9a6e6335962c.
I hope this helps!

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!