Note that there are some explanatory texts on larger screens.

plurals
  1. POGet time interval and play a sound in objective C
    primarykey
    data
    text
    <p>I am new to Object C, and I have two questions but I can not find the answers on stackoverflow.</p> <p>My iOS app is simple, one button on the screen, and if user tap it, it will:</p> <ol> <li><p>play a sound</p></li> <li><p>get the time interval between 2 taps in millisecond.</p></li> </ol> <p>Thanks to Owl, now the code to get the interval look like this:</p> <p>(Long coding since I don't understand what is "UNIX time stamp" and I don't know where/how to use the second code.)</p> <pre><code>double dt1; double dt2; -(IBAction)Beated:(id)sender{ If (FB == 1) { FB = 2; NSDate *date = [NSDate date]; NSTimeInterval ti = [date timeIntervalSince1970]; dt1 = ti; } else { FB = 1 NSDate *date = [NSDate date]; NSTimeInterval ti = [date timeIntervalSince1970]; dt2 = ti; double progress; progress = dt2 - dt1; int timeInMs = trunc(progress * 1000); NSLog(@"Interval %d", timeInMs); } } </code></pre> <p>And after start the app, there is a lag when the sound was played for the first time, but it works good after the first tap. How to stop that lag?</p> <p>My code to play the sound:</p> <p>in .h</p> <pre><code>#import &lt;AVFoundation/AVFoundation.h&gt; </code></pre> <p>and</p> <pre><code>AVAudioPlayer *audioPlayer; </code></pre> <p>in .m</p> <pre><code> -(IBAction)Beated:(id)sender { NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/ssn.wav", [[NSBundle mainBundle] resourcePath]]]; NSError*error; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:$error]; audioPlayer.numberOfLoops = 0; [audioPlayer play]; } </code></pre> <p>Thanks </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.
    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