Audio Record Object Empty when trying to start/stop with buttons in AppBuilder
조회 수: 6 (최근 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
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
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File 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!