Audio does not play

15 views (last 30 days)
N/A
N/A on 6 Mar 2016
Answered: Walter Roberson on 6 Mar 2016
[y, Fs] = audioread(filename);
a = audioplayer(y, Fs);
play(a);
This is the code I currently have for playing an audio file given the filename. It works when I run it in command line, but does not work when I run it as a part of GUI code. I've checked that the program goes through these lines of codes, but the only thing is that it does not play the sound it does when run in command line. How do I fix this?

Answers (1)

Walter Roberson
Walter Roberson on 6 Mar 2016
You are defining your audioplayer in the workspace of your callback function. As soon as that function returns, the audioplayer is deleted (along with all other variables in the workspace of the function.)
  • You can play the whole sound before returning by switching to playblocking() instead of play(); OR
  • You can add some code in the callback that arranges so that it does not return before the sound is finished, such as by adding pause() or appropriate waitfor(); OR
  • You can store a reference to the audioplayer outside the workspace so that it does not get deleted when the function returns; see http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.3F; OR
  • You can create the audioplayer before-hand and store it in an accessible location using the techniques in the above link, and then have your callback just play() it

Community Treasure Hunt

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

Start Hunting!