IPhone Sounds and Delays

I am developing an application where the user can click multiple hit areas that create sounds.
But the result is a little laggy, when multiple sounds are triggered at the same time, the sounds are played with an ugly delay.

I am using instances of AVAudioPlayer for each sound. Is there a better way to play sounds and prevent this lag?

Here's the code:

#import "MBImageView.h"
#import <AVFoundation/AVFoundation.h>

@implementation MBImageView

-(void)awakeFromNib
{
NSURL* audioFile = [NSURL fileURLWithPath[[NSBundlemainBundle] pathForResource:@"shaker" 
                                               ofType:@"caf"]]; 
    AudioServicesCreateSystemSoundID((CFURLRef)audioFile, &shortSound); 

}

- (id)initWithImage:(UIImage *)image{ 
    return self; 
}

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    AudioServicesPlaySystemSound(shortSound);
}


@end

      

Sincerely.

+2


source to share


3 answers


Most of these sounds (AVAudioPlayer and AudioServices) play after your runloop finishes. That is, you say that you are playing, and they queue up to play, and they don't start playing right away.

If you want free sound, you use the Audio Unit :

To ensure minimal latency, especially when logging in and out at the same time (for example, for a VoIP application), use an I / O module or an I / O module for speech processing. See "Audio Modules Support in iPhone OS."



You can also have a look at Audio Toolbox :

Use the Audio Toolbox environment to play audio with sync capabilities, access incoming audio packages, parse audio streams, convert audio formats, and record audio with access to individual packages. For more information see the Audio Toolbar Item Reference and SpeakHere sample project code.

+6


source


If these are short sounds that you don't mind loading into memory, C-based System Sound Services may suit you better.



+1


source


I've used the SoundEffect class in the Audio Toolbox with good results. My short sounds play without delay.

Also one more thing to consider with audio delays; make sure your audio files don't have "spaces" in front of the actual audio - I banged my head against the wall when I was looking for audio delay, only to find it in the actual audio file itself.

Hope it helps.

0


source







All Articles