I am trying to excute the following lines with MATLAB app designer
[x, fs] = audioread('wave.wav');
app.freq = 2 * fs;
player = audioplayer(x, app.freq);
play(player);
but no sound is produced. However, when I copy the above lines along with those lines,
restoredefaultpath
rehash toolboxcache
savepath
it sounds in the command window but not in the app designer.
How can I fix this issue? - TIA

 채택된 답변

Subhadeep Koley
Subhadeep Koley 2021년 4월 27일

1 개 추천

I have attached one demo minimum working example.
Have a look. It might help you fix the issue.
Also, if possible provide the full path of the sound file when reading it with audioread()

댓글 수: 5

Adham Elkhouly
Adham Elkhouly 2021년 4월 27일
편집: Adham Elkhouly 2021년 4월 27일
Hi - thank you for replying
I wrote this line in the app designer 2019b and 2021a
which audioplayer
and I got these lines in the command boxes
C:\Program Files\MATLAB\R2019b\toolbox\matlab\audiovideo\@audioplayer\audioplayer.m
C:\Program Files\MATLAB\R2021a\toolbox\matlab\audiovideo\audioplayer.m
unfortunately, still no sound in app designer 2019b or 2021a
your file produced a sound, but
load
doesn't accept .wav files
Instead of load() you can use
[app.x, app.fs] = audioread('wave.wav');
audioread doesn't work. This is the entire function
[filename, pathname]=uigetfile({'*.wav'},'File Selector');
fullpathname = fullfile(pathname, filename);
try
[app.x,app.fs] = audioread(fullpathname); %% handling exceptions in case you decide not to attach a folder
catch
return %% function terminates in case of an exception
end
app.wavFolderAvailableLamp.Color = 'g';
app.freq = 2 * app.fs;
player = audioplayer(app.x, app.freq);
play(player);
Am I doing something I shouldn't?
Define a public property player and store the audioplayer object inside app.player.
Then invoke play(app.player)
[filename, pathname]=uigetfile({'*.wav'},'File Selector');
fullpathname = fullfile(pathname, filename);
try
[app.x,app.fs] = audioread(fullpathname);
catch
return
end
app.wavFolderAvailableLamp.Color = 'g';
app.freq = 2 * app.fs;
app.player = audioplayer(app.x, app.freq); % Here app.player is a public property variable
play(app.player);

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adham Elkhouly
Adham Elkhouly 2021년 4월 27일

0 개 추천

For anyone facing a similar issue, I used
sound(app.x,app.freq);
instead of
player = audioplayer(app.x, app.freq);
play(player);

댓글 수: 2

@Adham Elkhouly I think, if you use sound(app.x, app.freq), then the audio clip will keep on playing even if you close the app window.
Whereas, using the below code, will stop the sound as soon as you close the app window.
app.player = audioplayer(app.x, app.freq);
play(app.player);
Yes, the sound keeps on with
sound()
and what you said works fine after I defined
player
as a public property - thank you very much

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2021년 4월 26일

댓글:

2021년 4월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by