How to make updating music-scroller and timer-string while is playing music in matlab R2012?
    3 views (last 30 days)
  
       Show older comments
    
    Temirkhan Amanzhanov
 on 10 Nov 2020
  
    
    
    
    
    Commented: Temirkhan Amanzhanov
 on 10 Nov 2020
            Hi everyone. I wanna make it by using timer function. I dunno what I need to set up into 'timer' function as 'TimerFcn', 'StartTimerFcn' etc. And also the function 'drawnow' doesn't make any sence. Thanks for your responds. 
2 Comments
  Walter Roberson
      
      
 on 10 Nov 2020
				Which sound player are you using? Some of them have built-in timer properties.
Accepted Answer
  Walter Roberson
      
      
 on 10 Nov 2020
        audioplayer has a TimerFcn property, and a TimerPeriod property to set the interval. For example,
t = (0:size(y,1)-1)./Fs;
ax = gca;
plot(ax, t, y);
xlim([0 5]);
obj = audioplayer(y, Fs);
obj.TimerFcn = @(hObject, event) ScrollTo(ax, hObject.CurrentSample./hObject.SampleRate);
obj.TimerPeriod = 0.5;
play(obj)
function ScrollTo(ax, SampleTime)
   set(ax, 'XLim', SampleTime + [0 5]);
   drawnow()
end
6 Comments
  Walter Roberson
      
      
 on 10 Nov 2020
				Using global variables and using "app" is not recommended for GUIDE. "app" is for App Designer.
See
http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
Also I suggest
    player.TimerFcn = @(hObject, event) ScrollTo(ax, hObject)
with
function ScrollTo(ax, hObject)
  handles = guidata(ax);
  SampleTime = hObject.CurrentSample./hObject.SampleRate;
  set(ax, 'XLim', SampleTime + [0 5]);
  handles.audioslider.Value = SampleTime;
  drawnow()
and remove the lines
    while (isplaying(player) == true)
        CurrSam = get(player, 'CurrentSample');
        set(handles.audioslider, 'Value', CurrSam);
        drawnow()
    end
as those are better done by the ScrollTo
Ah, though you should decide whether you want the audioslider to be by sample number or by seconds.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
