Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for sound to finish

Tags:

wait

matlab

audio

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  
like image 450
user552231 Avatar asked Dec 19 '12 05:12

user552231


2 Answers

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
}
like image 135
Esteban Avatar answered Oct 04 '22 21:10

Esteban


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!

like image 29
Mike Avatar answered Oct 04 '22 20:10

Mike