How to play audio files one by one by pushing the same button in GUI

조회 수: 2 (최근 30일)
Lucky
Lucky 2013년 6월 10일
댓글: John 2015년 8월 21일
Hi there! I am new to Matlab and gui especially, please help me! I need to play wav files one by one by pushing a button in gui. My files are in a folder, but I need to play them in a random way. I did permutations to shuffle them (this is because I need to know how exactly they shuffled, to make a statistics). In my opening function I wrote:
% --- Executes just before MYGUI is made visible.
function MYGUI_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 MYGUI (see VARARGIN)
% Choose default command line output for MYGUI
handles.output = hObject;
handles.status=0;
%inizialize global variable
global r
FoldData = dir('New Folder'); %Put metadata for folder in variable
FoldIndex = [FoldData.isdir]; %Gets list of files in folder
FileList = {FoldData(~FoldIndex).name};
FileList = FileList'; %Transpose resultant row vector into column vector
v = FileList';
P = perms(v); %make permutation of verctor v
r = P(9,:) %select 10th row (random choice)
r =
'comb3.wav' 'comb2.wav' 'comb4.wav' 'comb1.wav'
In my pushbutton callback (PLAYAUDIObutton) I have:
% --- Executes on button press in PLAYAUDIObutton.
function PLAYAUDIObutton_Callback(hObject, eventdata, handles)
% hObject handle to PLAYAUDIObutton(see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global r
if handles.status==0
handles.status=1;
k = r{1, 1}; %select 1st wav file from 10th row
[y,Fs]=wavread(k); %Import and play file
handles.audio = audioplayer(y,Fs);
play(handles.audio);
else
msgbox ('Test is finished. Thank you for your participation!');
end
guidata(hObject,handles);
But, this code is only for playing the very first audio file from my list (comb3.wav). Now I need to figure out how to play all 4 files one by one by pushing PLAYAUDIOpushbutton: I need to press button and hear comb3.wav, by pushing the same button second time to hear comb2.wav and etc. What function should I choose to play them sequentially? There could be not only 4 files, but around 20... Thank you in advance!!
  댓글 수: 4
Lucky
Lucky 2013년 6월 10일
Thank you, Daniel, now I hope the question is clear?
John
John 2015년 8월 21일
hallo everyone can anyone explain to me whats the meaning of handles.status==0??? in what help us????im new to this! thanks???

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

채택된 답변

Daniel Shub
Daniel Shub 2013년 6월 10일
If I understand your code correctly, every time you push the button the first file is played, but you want it to step through the list. Assuming you do not need the list for anything else you can chop the first item off the list by adding r = r{2:end}; (obviously with a check to make sure there are at least 2 items) at the end of your PLAYAUDIObutton_Callback function. If you need the list, you could set a new global (or persistent) counter in PLAYAUDIObutton_Callback

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by