How to set timer to execute a function
14 views (last 30 days)
Show older comments
Okay so this is what I'm trying to do. I'll put whatever I'm struggling with in parenthesis.
- MATLAB loads pre-recorded WAVE file (I can use Audioread but I need more help with that)
- MATLAB reads that pre-recorded WAVE file (Once, again, how?)
- If time is more than 60 seconds from initial, play WAVE file (how do I set this time thing)
- else if time is less than 60 seconds, do nothing (how do I set this time thing)
Thanks guys :)
0 Comments
Answers (1)
Geoff Hayes
on 15 Mar 2016
filename = 'myAudioFile.wav';
[y,Fs] = audioread(filename);
player = audioplayer(y,Fs);
We want to play the audio with a fixed delay, so we create a function that our timer will call
function playAudio(hObject, eventdata, audioPlayer)
play(audioPlayer);
We can ignore the first two input parameters as they are only there because we are going to call this function from a timer (which will populate them with the handle to the timer and perhaps some event data (which is typically empty)).
audioFilename = 'myAudioFile.wav';
[y,Fs] = audioread(filename);
player = audioplayer(y,Fs);
hTimer = timer('Name','MyAudioTimer', ...
'StartDelay', 60, ...
'TimerFcn',{@playAudio, player});
start(hTimer);
Try the above and see what happens!
2 Comments
Geoff Hayes
on 15 Mar 2016
You've combined playAudio function with the code that creates the timer. They are separate. The playAudio function just calls play on the audioPlayer object. The other code,
audioFilename = 'myAudioFile.wav';
[y,Fs] = audioread(filename);
player = audioplayer(y,Fs);
hTimer = timer('Name','MyAudioTimer', ...
'StartDelay', 60, ...
'TimerFcn',{@playAudio, player});
start(hTimer);
occurs outside of this function.
Also, the property of the timer is Name so you can't change it to the name of the wav file. Look to the documentation.
See Also
Categories
Find more on Audio and Video Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!