Control dsp.audiodevicewriter with a toggle switch button

1 次查看(过去 30 天)
Hello,
I would like to create a toggle button allowing user to chose when to start/stop my noise sample. (I attached a photo)
value of "stop" is '1' and value of "Play" is '2'
It starts correctly, but I can't stop the sound. I am using the app designer. Here is my code:
function replayone(app,event)
value = app.Switch.Value;
afr = dsp.AudioFileReader([app.dir '/audio_track/track' num2str(app.index) '.wav']);
Fs = afr.SampleRate;
adw = audioDeviceWriter('SampleRate', Fs);
app.counter = 0;
while ~isDone(afr)
audioIn = afr();
if app.counter == 0
click = ones([1,length(audioIn)])*0.3;
else
click = zeros*(1:length(audioIn));
end
t = (app.counter + 1 : app.counter + size(audioIn, 1));
app.counter = t(end);
audioout = [audioIn click'];
event.Source
if event.Value == '1'
break
end
adw(audioout);
end
delete(adw), delete(afr)
end
I have been trying quite a few condition, however it looks like when it enters in the loop "while ~isdone" it doesn't check the value of the toggle button anymore. I also have tried with 'switch' and different case but didn't resolve it. Thank you

采纳的回答

Walter Roberson
Walter Roberson 2018-1-8
The event queue will not be checked until a drawnow() or pause() or a figure() or a uiwait() or waitfor() .
Furthermore, the event parameter will not be updated because the function is not being invoked a second time. Instead of checking event.Value, you need to check app.Switch.Value

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by