Audio Record Object Empty when trying to start/stop with buttons in AppBuilder

33 次查看(过去 30 天)
I am trying to allow individuals to start and stop up to 180 seconds of audio recording from a microphone device.
When I run the following code, MATLAB uses my microphone but I get the error message: Unrecognized function or variable 'recObj' on the line 'stop(recObj) % Stop audio recording'.
No recordings are saved in my workspace. How can I fix this?
% Callback function: RECORDButton, UIFigure
function RECORDButtonPushed(app, event)
timenow = datestr(now,'hhMMss_ddmmyy');
Fs = 44100;
nBits = 16;
nChannels = 1;
ID = -1; % default audio input device
recObj = audiorecorder(Fs, nBits, nChannels, ID); %create audio object
record(recObj, 30); %start Recording
pause;
end
% Button pushed function: STOPButton
function STOPButtonPushed(app, event)
stop(recObj) % Stop audio recording
b=getaudiodata(recObj);
filename = ['newfile_', timenow,'.wav']';
audiowrite(filename, b, 44100);
end
  1 个评论
Angel Ceballos
Angel Ceballos 2019-11-1
Click on Property (red button on top-left corner) and add recObj like this:
properties(Access = public)
recObj;
end
Then change recObj to app.recObj on both functions:
% Callback function: RECORDButton, UIFigure
function RECORDButtonPushed(app, event)
timenow = datestr(now,'hhMMss_ddmmyy');
Fs = 44100;
nBits = 16;
nChannels = 1;
ID = -1; % default audio input device
app.recObj = audiorecorder(Fs, nBits, nChannels, ID); %create audio object
record(app.recObj, 30); %start Recording
pause;
end
% Button pushed function: STOPButton
function STOPButtonPushed(app, event)
stop(app.recObj) % Stop audio recording
b=getaudiodata(app.recObj);
filename = ['newfile_', timenow,'.wav']';
audiowrite(filename, b, 44100);
end

请先登录,再进行评论。

回答(1 个)

Ajay Pattassery
Ajay Pattassery 2019-10-30
Here the recObj is local to the function RECORDButtonPushed and its scope is limited to that function. Hence it is not accessible in the STOPButtonPushed function.
One of the solutions is to define the variable recObj as property and associate it to the app object. You can refer to the following document for further information.

类别

Help CenterFile Exchange 中查找有关 Audio I/O and Waveform Generation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by