How to set timer to execute a function
显示 更早的评论
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 :)
回答(1 个)
Geoff Hayes
2016-3-15
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 个评论
Geoff Hayes
2016-3-15
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Audio and Video Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!