user3305284
user3305284

Reputation: 43

How can I record,pause and keep recording with Matlab functions?

I am trying to record, pause, and record again an audio with this code, but it doesn't work true and only the last time I push the record, the sound recorded and before efforts lose.

How should I use pause and resume function to make a record/pause button?

function Rec_Pausb_Callback(hObject, eventdata, handles)
global openS recS;
openS=0; recS=1;
handles.recs=str2double(get(handles.Samplef, 'String'));
recb=str2double(get(handles.BPS, 'String'));

if(handles.Samplef >32 || handles.Samplef <8)
    disp('Frequency must between 8 to 32 (KiloHertz)');
else
   handles.Samplef=handles.Samplef;
end
disp(handles.Samplef);

state=get(hObject,'value');
handles.rec=audiorecorder(handles.recs*1000,recb,1);

if state
    set(hObject,'String','Pause');
    resume(handles.rec);
else
    set(hObject,'String','Record');
    pause(handles.rec);


end


guidata(hObject,handles);

Upvotes: 0

Views: 802

Answers (1)

Christian
Christian

Reputation: 61

I assume that the code you have posted above is what is happening in the callback function of the button?

Then a possible explanation could be, that the handles, and thus the handles.rec object, is not updated when the callback exits, and therefore the audio data is not saved. Try to add a guidata(hObject, handles); to the end of the callback in order to save the handles.

What version of Matlab are you using? The graphics system was updated in 2014b, so if you are using this version, the above solution might not be applicable.

EDIT: Okay, when looking at your complete code, you're initializing the audiorecorder object in the callback, effectively resetting it each time the button is pressed. This means that you start recording all over again whenever you push the button.

You can avoid this by initializing the recorder somewhere outside of the callback, and then pass the object to the button callback function.

Upvotes: 1

Related Questions