필터 지우기
필터 지우기

PLAY/STOP pushbutton

조회 수: 24 (최근 30일)
Ana Campos
Ana Campos 2019년 8월 3일
댓글: Walter Roberson 2019년 8월 4일
Hi!
I was wondering if anyone knows how to help me with this please.
Below is the code for a pushbutton that I am using to PLAY/STOP audio.
I need to be able to play and stop audio with the same button. That audio must be played in loop until the user pushes the button again. These two things the code is already doing.
My problem is: Once the user pushes the button and the audio stops, I need a way to allow the user to repeat the process as many times as he needs to by pushing the button again.
I understand why my code is not allowing me to repeat the process at the moment but I am not being able to think of a solution for what I need to do.
Thanks in advance for your help.
Cheers
% --- Executes on button press in playLOOP.
function playLOOP_Callback(hObject, eventdata, handles)
% hObject handle to playLOOP (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%generates random file
persistent check1
if isempty(check1)
check1 = 1
wavFileToPlay = handles.myWavFiles{handles.wavFileIndex}; % this is just to give the code the ID of a random song from my playlist, that will be played now
[x, fs] = audioread(handles.wavFileToPlay);
pause (0.1);
sound (x,fs)
set(handles.playLOOP,'string','STOP');
pause (11);
else ~isempty(check1)
%check1 = []
clear sound;
set(handles.playLOOP,'string','PLAY');
end

채택된 답변

Walter Roberson
Walter Roberson 2019년 8월 3일
The only known way to stop sound() from playing is to
clear sound
You should be looking at the pause() and resume() audioplayer methods, and you will need to use the audioplayer callbacks to detect end of sound to know to play() the object again in order to loop. There will generally be a delay in playing before the player restarts.
I would suggest that you instead look at the Audio System Toolbox in order to be able to stream audio.
  댓글 수: 7
Ana Campos
Ana Campos 2019년 8월 4일
Thanks!
Walter Roberson
Walter Roberson 2019년 8월 4일
% --- Executes on button press in playLOOP.
function playLOOP_Callback(hObject, eventdata, handles)
% hObject handle to playLOOP (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%generates random file
persistent player
if isempty(player)
wavFileToPlay = handles.myWavFiles{handles.wavFileIndex}; % this is just to give the code the ID of a random song from my playlist, that will be played now
[x, fs] = audioread(handles.wavFileToPlay);
player = audioplayer(x, fs);
set(handles.playLOOP,'string','STOP');
play(player);
else
stop(player)
set(handles.playLOOP,'string','PLAY');
player = [];
end
You still need to adjust this to have a callback on the audioplayer in order to trigger it to play in a loop. See the audioplayer documentation for callback properties.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Audio and Video Data에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by