If you are using the audioplayer to play the music, you can always assign a periodic timer to the player so that every one tenth of a second (or whatever your period is) you can update the slider position. For example, suppose we have a GUI with an axes, a push button, and a slider. The push button callback would start the audio and update the axes with the sample data, and could be written something like
function pushbutton1_Callback(hObject, eventdata, handles)
load handel;
handles.player = audioplayer(y, Fs);
handles.y = y';
handles.Fs = Fs;
handles.timeSec = length(y)/Fs;
handles.atSample = 0;
set(handles.player,'TimerFcn',{@timerCallback,handles.figure1}, 'TimerPeriod', 0.1);
guidata(hObject,handles);
cla(handles.axes1);
hold(handles.axes1,'on');
xlim(handles.axes1,[1 length(y)]);
play(handles.player);
We simply initialize the player, assign a timer function, save the updated handles object, and play the audio. The timer callback would look something like
function timerCallback(hObject, event, hFig)
handles = guidata(hFig);
currSample = get(hObject,'CurrentSample');
data = handles.y(handles.atSample+1:currSample);
plot(handles.axes1,handles.atSample+1:currSample,data);
handles.atSample = currSample;
guidata(hFig,handles);
if currSample > 1
sliderVal = min(1.0,currSample/length(handles.y));
set(handles.slider1,'Value',sliderVal);
end
Given that our timer period is 0.1 seconds, then whenever the timer callback fires we update the axes with the sample data, and update the position of the slider. (We use a percentage of the "read" samples to determine its position.)
See the attached GUI which implements the above.
Best Answer