Stop audio from playing using sound()

Matlab Online (ver R2018b)
I'm playing some audio using sound(y,Fs) but I can't find a way to stop the audio from playing. You can guess that it gets even worse when you (accidentally) execute the command twice (yes it plays the music over eachother instead of replacing it). Refreshing the page is the only option for now.
p.s.
I know you can use audioplayer(y,Fs) for offline versions but it won't work online.

1 commentaire

Jan
Jan le 11 Mar 2022
The sound() command does call audioplayer() internally in R2018b. So I cannot see a reason to prefer sound() instead or using audioplayer() directly.

Connectez-vous pour commenter.

 Réponse acceptée

Walter Roberson
Walter Roberson le 12 Fév 2019

1 vote

sound() can only be interrupted by killing matlab .

4 commentaires

Shira Labin
Shira Labin le 17 Déc 2020
PLease continue reading, this answer is not accurate (use: clear sound;)
noam gridish
noam gridish le 8 Août 2021
hey, do you know how to stop specific sound?
Walter Roberson
Walter Roberson le 8 Août 2021
sound() keeps a persistent variable that is a vector of audioplayer objects. Each time you use sound(), it looks at its existing vector and destroys the ones that have stopped playing. Then it creates an audioplayer object and adds it to the end of the list; it does not record any identification information such as a unique tag when it does that.
In order to stop a specific sound() you would need to get access to the list of audioplayer objects that is stored inside the function, at the same time ignoring any audioplayer objects that were created outside the function (because those ones cannot be the right sound.)
Unfortunately, audioplayer does not derive from handle class, and audioplayer class does not keep track of audioplayer objects, and mathworks does not provide any documented method of accessing the persistent variables inside a function. It is not obvious that the objects can be examined at all.
There just might be a way using the kinds of ideas outlined in https://www.mathworks.com/matlabcentral/answers/474974-how-can-i-find-all-object-of-a-certain-handle-class#answer_386321 but note that is talking about handle classes and audioplayer is not a handle class.
If, hypothetically, you were able to locate the audioplayer objects, then you run into the problem that the objects do not have any tags or identifiers. If you knew the size() of the data that was being played, you might be able to reduce the search by removing from consideration the ones that do not have the right size, but you would pretty much have to compare passed-in sound to the data stored in the audioplayer object in order to determine whether you had the right object.
It seems to me that it would be a lot easier to just use audioplayer() yourself and keep track of the objects.
Jan
Jan le 11 Mar 2022
@Walter Roberson: Do you agree to accept Joe V's answer, because it is working?

Connectez-vous pour commenter.

Plus de réponses (2)

Joe V
Joe V le 27 Juin 2019

64 votes

6 commentaires

Gabriel Rantung
Gabriel Rantung le 4 Avr 2020
Thank you so much!
Kiana Maillet
Kiana Maillet le 1 Déc 2020
thank you !!!!
Marcel-Dennis Boerzel
Marcel-Dennis Boerzel le 22 Fév 2022
works. Thanks!!!
Nguyen Nam
Nguyen Nam le 13 Avr 2024
Thanks.
Alberth Tamo
Alberth Tamo le 3 Mai 2024
Works very well
Theodore
Theodore le 22 Sep 2025
Thank you.

Connectez-vous pour commenter.

lol u guys can also use this too...
[a, fs] = audioread('yourfilehere.mp3');
plot (a(1:100000,1))
fs =1.0*fs; %%We can adjust our speed of our song by multiplying our fs with any desired numbers.
sound (a,fs);
y = input ('Press 1 to stop the music: ');
while y~=1
fprintf ('You didn''t enter 1 ');
fprintf ('\n');
end
clear sound;

2 commentaires

Walter Roberson
Walter Roberson le 6 Jan 2021
If the person does not enter 1, then your while is an infinite loop.
Rik
Rik le 6 Jan 2021
Also, the relevant code is equivalent to the other answer. What does this one add?

Connectez-vous pour commenter.

Produits

Version

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by