Max - rather than using the sound function to play the audio recording, consider using an audio player object which provides the ability to stop the audio playback. For example, in your play button callback, you could do
function pushbutton1_Callback(hObject, eventdata, handles)
fs = 44100;
t = 0:1/fs:10;
f = 440;
y = cos(2.*pi.*f.*t);
handles.hAudioPlayer = audioplayer(y,fs);
guidata(hObject, handles);
play(handles.hAudioPlayer);
The last three lines in the above code, we create the player, save the updated handles structure (which has a field for the handle to the audio player), and then start the (non-blocking) playback.
Now, in the stop button callback, you could do
function pushbutton3_Callback(hObject, eventdata, handles)
if isfield(handles,'hAudioPlayer')
stop(handles.hAudioPlayer);
end
We check to see if there exists an audio player handle field in the handles object, and if so, then we just stop the playback.
Try the above and see what happens!
Best Answer