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 日

1 投票

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 月 3 日
Thanks for your answer. In case I need to use sound, can you think of any way of writing a loop that allows me to do it?
If you can’t, how could I use audioplayer? I’m using gui and I tried what you said but I couldn’t make it work with handles.
Cheers
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 playing
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','PAUSE');
start(player)
playing = true;
elseif playing
pause(player)
playing = false;
set(handles.playLOOP,'string','RESUME');
else
resume(player)
playing = true;
set(handles.playLOOP,'string','PAUSE');
end
However this needs to be enhanced so that at the end of the clip, it automatically plays the clip again. You can do that by configuring a callback for audioplayer .
Note: I interpreted your requirements as being about pausing the playing and resuming from the paused position. An alternative interpretation would be that each time the user asks to play, that it should restart from the beginning of the sound. A third interpretation is that each time the user asks to stop / play, that you are wanting to change songs. All three are valid interpretations, and you will need to decide which one you want.
Ana Campos
Ana Campos 2019 年 8 月 4 日
Hi! thanks :)
Im using MATLAB R2015b (in case the error has something to do with it) and I keep getting an error and the code is still not working with audioplayer:
Undefined function 'start' for input arguments of type 'audioplayer'.
Im sorry if it's basic but I have been writing the code for this project for a long time and I think my brain collapsed here and this is the last thing I need to do.
The following would be the right interpretation:
"An alternative interpretation would be that each time the user asks to play, that it should restart from the beginning of the sound."
Thank you again for your help Walter.
Cheers
Walter Roberson
Walter Roberson 2019 年 8 月 4 日
Sorry should be play(player) not start(player)
Walter Roberson
Walter Roberson 2019 年 8 月 4 日
I will adjust for the other need after I sleep.
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 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by