Note that there are some explanatory texts on larger screens.

plurals
  1. POPlaying sound at interval with GCD timer not behaving as expected
    primarykey
    data
    text
    <p><strong>Hi !</strong></p> <p>I'm building a timer using GCD for the purpose of playing a sound at a specific interval, to be more precise, it's a metronome sound. I've been trying for days to solve my issue but nothing. Everything is good but when I set my tempo to a bigger value , let's say 150 bpm or 200 bpm, when the sound starts for the first time, it fires very quickly(almost like two sounds in the same time meaning it does not have the expected interval) and after this , it calibrates. I start the sound the second time , all is good... so this happens only the first time I resume my dispatch source so I'm guessing it has something to do with loading the sound from the disk , like in this post : <a href="https://stackoverflow.com/questions/900461/slow-start-for-avaudioplayer-the-first-time-a-sound-is-played">Slow start for AVAudioPlayer the first time a sound is played</a> . For my sound I used at first an instance of <code>AVAudioPlayer</code> with <code>prepareToPlay</code> and <code>play</code> and also created it in the AppDelegate class, it hasn't work...I have even tried the SoundManager class developed by @NickLockwood,same issue. At present, I'm using a <code>SystemSoundID</code>. As for the timers, this is my first GCD timer , I've already tried the classical <code>NSTimer</code>, <code>CADisplayLink</code> and other timers found on git... all in vain.</p> <p>Another interesting issue is that with the other timers , everything is perfect on the simulator but on the device the same glitch.</p> <p>Here's the code, I hope someone will bring me to the light.</p> <pre><code> -(void)playButtonAction // { if (_metronomeIsAnimatingAndPLaying == NO) { [self startAnimatingArm]; // I start my animation and create my timer metronomeTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)); dispatch_source_set_timer(metronomeTimer,dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC),duration * NSEC_PER_SEC,duration *NSEC_PER_SEC); dispatch_source_set_event_handler(metronomeTimer, ^{[self playTick];}); dispatch_resume(metronomeTimer); _metronomeIsAnimatingAndPLaying = YES; } } -(void)playTick { AudioServicesPlaySystemSound(appDeleg.soundID); // soundID is created in appDelegate } </code></pre> <p>In my application didFinishLaunching </p> <pre><code>NSString *path = [[NSBundle mainBundle] pathForResource:@"tick" ofType:@"caf"]; AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path] , &amp;_soundID); </code></pre> <p>And BPM setter and getter :</p> <pre><code>- (NSUInteger)bpm { return round(60.0 / duration); } - (void)setBpm:(NSUInteger)bpm { if (bpm &gt;= MaxBPM) { bpm = MaxBPM; } else if (bpm &lt;= MinBPM) { bpm = MinBPM; } duration = (60.0 / bpm); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload