How to set timer to execute a function

12 ビュー (過去 30 日間)
BeestMann
BeestMann 2016 年 3 月 3 日
コメント済み: Geoff Hayes 2016 年 3 月 15 日
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
Geoff Hayes 2016 年 3 月 15 日
Beestmann - audioread can be used to read the audio data from file as
filename = 'myAudioFile.wav';
[y,Fs] = audioread(filename);
Now, you can use the audio player to load (and later play) the audio as
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)).
Now we create a (one-off) timer to play the audio file in sixty seconds as
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 件のコメント
BeestMann
BeestMann 2016 年 3 月 15 日
編集済み: BeestMann 2016 年 3 月 15 日
function BRUHHH = playAudio(hObject, eventdata, audioPlayer)
play(audioPlayer);
Filename = 'imperial_march.wav';
[y,Fs] = audioread(Filename);
player = audioplayer(y,Fs);
hTimer = timer('imperial_march.wav','MyAudioTimer', ...
'StartDelay', 60, ...
'TimerFcn',{@playAudio, player});
start(hTimer);
end
So this is what I put on the function tab. I saved it as "BRUHHH.m" - what did I do wrong? MATLAB is saying that there aren't enough input arguments on lines 2 and I can't figure out what to put in 'Name'
Also, there are too many input arguments for the audioread part? What's wrong with that? Thanks :)
Geoff Hayes
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 ExchangeAudio and Video Data についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by