How can I record an audio from a tuggle button in the GUIDE ?

1 view (last 30 days)
I have to make a voice recorder using GUIDE.
I don´t know if I can put the "audiorecorder object" in the handles, the code below is in the CallBack function of tuggle button , when I push the toggle button it gives me an error, it says "record is empty". I use "getaudiodata" because I also have to modify the voice. I need that record the voice, stop the record and save that in a vector.
handles.x = get(hObject,'Value')
handles.audio = audiorecorder(44100,16,1);
if handles.x == 1
record(handles.audio);
elseif handles.x == 0
stop(handles.audio);
y = getaudiodata(handles.audio);
audiowrite('Voz.wav',y,44100)
end
guidata(hObject, handles)
This is the error...
Error using audiorecorder/getaudiodata (line
765)
Recorder is empty.
Error in ProyectoS>togglebutton1_Callback
(line 136)
y = getaudiodata(handles.audio);

Answers (1)

Geoff Hayes
Geoff Hayes on 1 Aug 2020
Samuel - I think that the problem is that you are creating a new audio player each time the callback is called:
handles.audio = audiorecorder(44100,16,1); % <--- creating this again
if handles.x == 1
record(handles.audio);
elseif handles.x == 0
Instead, try creating the recorder only if the button is toggled on:
if get(hObject,'Value') == 1
handles.audio = audiorecorder(44100,16,1);
record(handles.audio);
elseif get(hObject,'Value') == 0
stop(handles.audio);
y = getaudiodata(handles.audio);
audiowrite('Voz.wav',y,44100)
end
guidata(hObject, handles)
I removed the handles.x since that isn't strictly needed (unless some other callback is interested in this information).

Community Treasure Hunt

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

Start Hunting!