Playing a Sound File

Discussion in 'iPhone/iPad Programming' started by HarryWorksInc, Aug 19, 2010.

  1. macrumors regular

    Joined:
    Feb 21, 2010
    #1
    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:
    HTML:
    [B].h[/B]
    @protocol GameViewControllerDelegate;
    
    #import <UIKit/UIKit.h>
    #import <AVFoundation/AVFoundation.h>
    
    @interface GameViewController : UIViewController <UIAccelerometerDelegate>{
    	AVAudioPlayer *HealthSound;
    }
    @property (nonatomic, assign) id <GameViewControllerDelegate> delegate;
    
    @end
    
    [B].m[/B]
    
    -(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;[B]
    		HealthSound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"HealthPowerUp" ofType:@"mp3"]] error:NULL];
    		[HealthSound play];[/B]
    	}
    }
    
    Does anyone know of a way of playing a short sound without causing my ame to halt momentarily?

    Thanks
     
  2. macrumors newbie

    Joined:
    Jun 21, 2010
    #2
  3. thread starter macrumors regular

    Joined:
    Feb 21, 2010
    #3
    Thank you very much that seems to work.
     

Share This Page