Note that there are some explanatory texts on larger screens.

plurals
  1. POAccurate repeating iPhone sounds
    primarykey
    data
    text
    <p>I am working with an app that needs to play a sound "tap" at 60 ms intervals (making for 1000 taps per minute). When I get past about 120 ms intervals (500 taps per minute), the sounds become very erratic and seem to be piling up on one another.</p> <p>The sound byte that I'm using is 10 ms long, and I have a thread timer that is running at 20 ms intervals. I am using AVAudioPlayer to play the sound, and have tried formats of wav, caf and m4a. I have tested with NSLog outputs to see when the AVAudioPlayer is being fired off, and when the timer loop is polling...and all the times seem accurate enough.</p> <p>I have added Audio Session handling in my viewDidLoad() method as follows:</p> <pre><code> [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &amp;setCategoryError]; </code></pre> <p>Here is my timer loop that is running in it's own thread:</p> <pre><code>NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Give the sound thread high priority to keep the timing steady. [NSThread setThreadPriority:1.0]; while (running) { // loop until stopped. [self performSelectorOnMainThread:@selector(playOutput) withObject:nil waitUntilDone:NO]; // An autorelease pool to prevent the build-up of temporary objects. NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init]; NSDate *curtainTime = [[NSDate alloc] initWithTimeIntervalSinceNow:sleepValue]; NSDate *currentTime = [[NSDate alloc] init]; // Wake up periodically to see if we've been cancelled. while (running &amp;&amp; ([currentTime compare:curtainTime] == NSOrderedAscending)) { [NSThread sleepForTimeInterval:0.015]; [currentTime release]; currentTime = [[NSDate alloc] init]; } [curtainTime release]; [currentTime release]; [loopPool drain]; } [pool drain]; </code></pre> <p>Here is how I am initializing my AVAudioPlayer:</p> <pre><code>NSString *beatOne = @"3h"; NSString *beatOneSoundFile = [[NSBundle mainBundle] pathForResource:beatOne ofType:@"caf"]; NSURL *beatOneURL = [[NSURL alloc] initFileURLWithPath:beatOneSoundFile]; beatPlayerOne = [[AVAudioPlayer alloc]initWithContentsOfURL:beatOneURL error:nil]; beatPlayerOne.delegate = self; beatPlayerOne.currentTime = 0; [beatPlayerOne prepareToPlay]; [beatOneURL release]; </code></pre> <p>And here is where the audio player is played:</p> <pre><code>[beatPlayerOne pause]; beatPlayerOne.currentTime = 0; [beatPlayerOne play]; </code></pre> <p>Thanks in advance for any help...</p>
    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