How to use audioplayer/play in AppDesiner

3 views (last 30 days)
I would like to use audioplayer and play function in the callback of AppDesigner. However, I cannot hear anything when playing in AppDesigner. If I use "sound" function, it works correctly, but "play" function dose not work to play sound.​
% Button pushed function: Button
function ButtonPushed(app, event)
load handel.mat;
playerObj = audioplayer(y,Fs);
start = 1;
stop = playerObj.SampleRate * 3;
play(playerObj,[start,stop]); % This does not work.
% load gong.mat;
% sound(y); % This works.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 20 Feb 2023
Edited: MathWorks Support Team on 20 Feb 2023
The root cause is that with audioplayer, when the callback exits, the audioplayer variable is deleted because it goes out of scope. Deleting it stops the playing.
To continue playing audio after the callback exits, use the playblocking function instead of the play function as below.
% Button pushed function: Button
function ButtonPushed(app, event)
load handel.mat;
playerObj = audioplayer(y,Fs);
start = 1;
stop = playerObj.SampleRate * 3;
playblocking(playerObj);
You can refer to further information about the above explanation at the following URL.

More Answers (0)

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!