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

2 次查看(过去 30 天)
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.

采纳的回答

Paulo Silva
Paulo Silva 2011-6-25
%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 个评论
Daniel Shub
Daniel Shub 2011-6-27
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 2011-6-30
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!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Audio I/O and Waveform Generation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by