Creating a microphone GUI
4 views (last 30 days)
Show older comments
Hi, I am an almost completely new matlab user, and I'm trying to create a very simple microphone GUI that has three buttons: a record button to stop recording, a stop button to stop recording, and a playback button to playback the recorded audio. I cobbled this code together from another similar question. First, I created handles for the fs and the recorder
function recorderTest_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to recorderTest (see VARARGIN)
% Choose default command line output for recorderTest
handles.output = hObject;
%setting sample rate
handles.fs=8192;
%creating the recorder
handles.recorder=audiorecorder(handles.fs,8,1);
% Update handles structure
guidata(hObject, handles);
Then, I programmed each button
% --- Outputs from this function are returned to the command line.
function varargout = recorderTest_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in record.
function record_Callback(hObject, eventdata, handles)
% hObject handle to record (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
recorder=handles.recorder
record(recorder)
guidata(hObject,handles)
% --- Executes on button press in stop.
function stop_Callback(hObject, eventdata, handles)
% hObject handle to stop (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
recorder=handles.recorder
stop(recorder)
guidata(hObject,handles)
% --- Executes on button press in playback.
function playback_Callback(hObject, eventdata, handles)
% hObject handle to playback (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
recorder=handles.recorder
play(recorder)
guidata(hObject,handles)
end
When I run it, and hit record, pause, and play in that order, the audioplayer properties popup, but I don't hear anything when I press play. I would actually prefer an error because then I would know what to look for! Any help would be appreciated.
0 Comments
Answers (1)
Geoff Hayes
on 19 Sep 2016
Austin - I suspect that the audio is being played but then terminated immediately when the playback_Callback returns. You could add a pause call after you have called play so that the function does not return until the playback completes, but then you would have to determine how long to pause.
A better solution would be to use an audioplayer for the playback since it has a playblocking function to hold control until audio playback has completed. Your code would then become
function playback_Callback(hObject, eventdata, handles)
myAudioPlayer = audioplayer(getaudiodata(handles.recorder),handles.fs);
playblocking(myAudioPlayer);
Try the above and see what happens! Note that there is no need call guidata in any of your pushbutton callback functions since there is no code that modifies the handles object. You only need to call
guidata(hObject,handles)
if you update the handles structure.
0 Comments
See Also
Categories
Find more on Audio and Video Data 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!