0
votes

I already asked this question to no avail. I couldn't solve the problem. I am trying again maybe someone can help!

I have 2 buttons, Play and Stop. I would have the path of an audio track at hand and want the audio to play when the user clicks on play and stop when the user clicks on stop. The only problem is that once the callback function of play exits, the variable holding the info about the player is emptied, and hence the track does not play. I needed some method to share data between callback functions..

I tried using global variables to no avail..

then I looked at this and I tried more or less each and every mentioned method: http://www.mathworks.com/help/matlab/creating_guis/share-data-among-callbacks.html#bt9p4qp

Let's take for instance this method:

In the play callback button:

[Y, Fs] = audioread(path);
player = audioplayer(Y,Fs);
hObject.UserData = player;
play(player);

In the stop callback button:

h = findobj('Tag','Play');
player = h.UserData;
stop(player);

On hObject.UserData = player; I am getting this warning when clicking on the play button:

Warning: Struct field assignment overwrites a value with class "double". See MATLAB R14SP2 Release Notes, Assigning Nonstructure Variables As Structures Displays Warning, for details.

The solution I need would let me keep using the rest of the program while the music is playing, stop the audio whenever I want, and the program must obviously keep working well after that.

Any ideas guys? any help would be truly appreciated!

Thanks in advance!

1

1 Answers

0
votes

I'm not shure whether the following works:

hObject.UserData = player;

I would (as you have already found out) use a global variable. I didn't test this solution, but it should work and shows how to use global variables in combination with a GUI correct. Please correct me if you found a mistake.

% --- Executes just before GUI is made visible.
function GUI_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)
% varargi

% Create a global player variable
global player;
player = audioplayer(Y,Fs);; % initialize player


% --- Outputs from this function are returned to the command line.
function pushbutton_off(hObject, eventdata, handles)
global player; % tell matlab player is global
play(player)

% --- Outputs from this function are returned to the command line.
function pushbutton_on(hObject, eventdata, handles)
global player;
stop(player);