I am playing a sound using sound command in Matlab and I want for the program to wait until it finishes playing the sound before executing the next command. How can I do it?
>>tic
>>sound(signal,Fs)
>>wait??
>>b=toc
You can use the audioplayer function:
%Create player object
player = audioplayer(signal, Fs);
%play sound
play(player)
while( strcmp(player.running,'on') )
{
% Waiting for sound to finish
}
I had a similar issue and tried to use the Estaban's suggested answer, but I couldn't get my while loop right (I am a novice programmer) and the script kept hanging. Eventually I stumbled upon the playblocking function, which seems to do exactly what the OP wanted - to pause the code until the player is done playing the sound. So, to modify Esteban's previous answer, use the "playblocking" function in place of the "play" function. Then the While loop is not needed!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With