1
NSString *path = [[NSBundle mainBundle] pathForResource:@"hit-pipe" ofType:@"wav"];
audioPlayer =[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
[self.audioPlayer setDelegate:self];
[self.audioPlayer prepareToPlay];
[self.audioPlayer play]; 

It takes time to load the sound for the first time.

Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
Varghese
  • 121
  • 2
  • 5
  • 15
  • Check [this](http://stackoverflow.com/questions/900461/slow-start-for-avaudioplayer-the-first-time-a-sound-is-played) answer for delay. Solution is [this](http://stackoverflow.com/questions/5684374/trying-to-fix-avaudioplayer-lag-on-its-initial-use) and [this](http://stackoverflow.com/questions/2550480/delay-in-playing-sounds-using-avaudioplayer) links. Get helped – Paresh Navadiya Aug 13 '12 at 06:29

2 Answers2

0

It takes time to load because it is loading the file. There will always be a tiny bit of usually unnoticeable lag when you play an AVAudioPlayer, and more lag when you load the file. How much lag depends on the size and file format of your file. If it is a problem, try allocating and loading your AVAudioPlayer earlier or using a smaller sound file.

WolfLink
  • 3,308
  • 2
  • 26
  • 44
0

You could put the code to load when the app is loading (for example in init or viewDidLoad). Then call audioplayer play when you need to play it. I'm doing this for my app and I did not notice any delay. It plays instantly when I call it.

Klipnov
  • 105
  • 1
  • 9