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

13 ビュー (過去 30 日間)
Olly Robertson
Olly Robertson 2019 年 10 月 27 日
コメント済み: Angel Ceballos 2019 年 11 月 1 日
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 Center および File ExchangeAudio and Video Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by