tags:

views:

1080

answers:

3

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
+3  A: 

Never used "sound()" but when I have played audio using wavplay(..., ..., 'async') I can stop the sound by issuing

clear playsnd

Maybe that works with sound() as well? Note: This is when playing asynchronously. For synchronous playback I assume that CTRL-C should break it, but I had issues with wavplay() last time I tried that.

kigurai
Just tried this: started audio using "sound()" and issuing clear playsnd stop the audio. Nice!
S.C. Madsen
+3  A: 

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)
Jacob
Damn, I was 8 sec too late ;)
Gacek
Lol, it's hilarious how everyone seems to have woken up at answered this question at around 1000 EST :)
Jacob
Just as I hit the button to post my answer, I got the orange banner saying other answers were posted. =D
gnovice
Indeed :) more than 30 minutes without any answer, and then, suddenly, 3 at almost the same time :D And you really do think...
Gacek
Sorry for my late response, but I wanted to try out the suggested solution before selecting an answer. The solution jacob proposes works, and thank you for the suggestion. However, the solution suggested by kigurai I think is more appropriate for the question: Kill sound(). So I'm selecting kigurai's response as answer to my question. Thanks again!
S.C. Madsen
A: 

Use the audioplayer object instead - it gives you the full control on what you do with the sound. I.e:

player = audioplayer(x, fs);
play(player)   % start the player
stop(player)   % stop whenever you like...

Audioplayer has a lot of other useful stuff: http://www.mathworks.com/access/helpdesk/help/techdoc/ref/audioplayer.html

Gacek

related questions