Alex Rivera | Logout

How to stop sound in MATLAB?

Asked 2009-11-16T13:41:39.440
36

When playing a sound using e.g:

sound(x,fs);

I sometimes by accident play the wrong one. If x is of substantial length, I currently try to wait until the sound has completed. Any suggestions on how to "abort" the playback? I've already tried

sound(mute,fs); % Mute is a short vector containing all zeroes

But that didn't work. I'm using Windows by the way.

UPDATE:
The following solution proposed by kigurai seems to do the trick:

sound(x,fs); % Start the audio

Now kill audio by

clear playsnd
Edit
Report

1 Answer

28

Mathworks says (and this applies to sound as well),

There is no function in MATLAB that can pause or stop audio playback once initiated by WAVPLAY. Instead of using WAVPLAY, an alternative is to create an AUDIOPLAYER object. This type of object has methods which allow pausing, resuming and stopping the audio playback. For example:

player = audioplayer(Y, Fs)

% start the playback
play(player);

% pause the playback
pause(player);

% resume the playback
resume(player)

% stop the playback
stop(player)
answered 2009-11-16T15:04:50.240

Your Answer