PDA

View Full Version : Playing a Sound File




HarryWorksInc
Aug 20, 2010, 01:10 AM
I am currently working on a game and have just begun adding sound effects but i have noticed that when i play the sound all of the animations in my game freeze. This is how I am playing the sound:
.h
@protocol GameViewControllerDelegate;

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface GameViewController : UIViewController <UIAccelerometerDelegate>{
AVAudioPlayer *HealthSound;
}
@property (nonatomic, assign) id <GameViewControllerDelegate> delegate;

@end

.m

-(void)tapHealth {
if (Playing && CollectedShield==NO && CollectedJetPack==NO) {
if (Health>60) {
Health=100;
} else {
Health += 40;
}
NSString *HealthString = [[NSString alloc] initWithFormat:@"%d", Health];
HealthLabel.text = HealthString;
HealthSylmbol.hidden = YES;
HealthSound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"HealthPowerUp" ofType:@"mp3"]] error:NULL];
[HealthSound play];
}
}

Does anyone know of a way of playing a short sound without causing my ame to halt momentarily?

Thanks



DennisVar
Aug 20, 2010, 05:00 AM
See this thread: http://stackoverflow.com/questions/900461/slow-start-for-avaudioplayer-the-first-time-a-sound-is-played

You can for example, [[AVAudioPlayer alloc] init]; in your application:didFinishLaunchingWithOptions: and then immediately release it to eliminate the first time lag.

HarryWorksInc
Aug 21, 2010, 09:43 AM
Thank you very much that seems to work.