Having problem in placing a timer behind a togglebutton for audio recording!

1 view (last 30 days)
Dear All,
Actually I have a toggle button (togglebutton1). When I click on it first time, it starts recording. The next time I click it, it stops recording. This works fine with the following code:
function togglebutton1_Callback(hObject, eventdata, handles)
button_state = get(hObject,'Value');
if button_state == get(hObject,'Max')
recorder = audiorecorder(8000,8,1);
record(recorder);
set(handles.togglebutton1,'String','Stop Recording');
handles.recorder=recorder;
guidata(hObject,handles);
elseif button_state == get(hObject,'Min')
recorder=handles.recorder;
stop(recorder);
samples = getaudiodata(recorder,'uint8');
set(handles.togglebutton1,'String','Start Recording');
end
But what I want is that when you click on it for the first time, it starts recording. Then you can't press the button for 2 seconds. After 2 seconds, you can press the button to stop recording. This will make my recording duration of atleast 2 seconds. The recording could be greater than 2 seconds but the recording shouldn't be less than 2 seconds.
So, what I want is when you click on the togglebutton, it starts recording. Then the togglebutton gets disabled (enable=off) for 2 seconds. After atleast 2 seconds or more than 2 seconds recording, you can press the togglebutton again to stop recording.
What modifications would you suggest in the mentioned code?
Need it urgently.
Thank you.

Accepted Answer

Paulo Silva
Paulo Silva on 25 Jun 2011
%code to start recording and start record
set(hObject,'enable','off')
pause(2)
set(hObject,'enable','on')
%now the user can click again on the button
  2 Comments
Daniel Shub
Daniel Shub on 27 Jun 2011
You may also want to add something like this to the code to stop recording. If the user clicks to stop recording and then very quickly clicks to start recording again, the soundcard will likely crash. A pause of about 0.5 seconds should enough time to allow the hardware to reset.
Matlab Help Seeker
Matlab Help Seeker on 30 Jun 2011
Hi Daniel,
I tried doing this but it works fine on my PC. But I could consider adding it keeping in mind that some PCs don't have much resources like mine :)
Thanks for the suggestion!

Sign in to comment.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!