How to set timer to execute a function
조회 수: 12 (최근 30일)
이전 댓글 표시
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
답변 (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.
참고 항목
카테고리
Help Center 및 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!