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

34 Ansichten (letzte 30 Tage)
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 Kommentar
Angel Ceballos
Angel Ceballos am 1 Nov. 2019
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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Ajay Pattassery
Ajay Pattassery am 30 Okt. 2019
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.

Kategorien

Mehr zu Audio I/O and Waveform Generation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by