MATLAB: Cant SOUND be interrupted using Ctrl-C

ctrl-cmacMATLABsound

When an audiofile is played using the SOUND command, it cannot be stopped using Ctrl-C.
According to solution 1-UAQTF, this was fixed in R2011b, but it doesnt seem to work in R2013a.

Best Answer

There are a couple of ways for achieving the ability to interrupt a playing audio file. The first way is by creating an object for playing audio using the AUDIOPLAYER command and then using PLAY and STOP commands to play or stop playing the file. Further, the PAUSE command can be used here to pause the audio file at a certain instant and the RESUME command can be used to resume playing from that instant. Below is an example that illustrates this using a sample audio file of Handel’s “Hallelujah Chorus”.
>>load handel;
>>player = audioplayer(y, Fs);
Now type this at the command prompt to play the file:
>>play(player)
To stop playing type this at the command prompt:
>>stop(player)
Also, to pause playing at any instant:
>>pause(player)
Then to resume playing after pause:
>>resume(player)
For further details on AUDIOPLAYER, please refer to the following documentation:
The second way is to use PLAYBLOCKING to play audio from an AUDIOPLAYER object that is created using the AUDIOPLAYER command. In this case, the audio file can be interrupted by using Ctrl-C. An example is shown below which also uses the sample audio file of Handel’s “Hallelujah Chorus”.
>>load handel;
>>playblocking(audioplayer(y,Fs))
To stop at any instant, use Ctrl-C.
If an audioplayer object is created explicitly then use a function instead to be able to interrupt using Ctrl+C.
This would not need the clearing of the audioplayer object using CLEAR command which is otherwise needed after hitting Ctrl+C to stop playing.
Example code:
function testAudio
fs = 16000;
duration = 30;
x = randn(duration*fs,1);
x = x/max(abs(x));
ap = audioplayer(x,fs);
playblocking(ap);
end
>>testAudio