Audio Record Object Empty when trying to start/stop with buttons in AppBuilder
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Angel Ceballos
el 1 de Nov. de 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
Respuestas (1)
Ajay Pattassery
el 30 de Oct. de 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.
0 comentarios
Ver también
Categorías
Más información sobre Audio I/O and Waveform Generation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!