I've got a problem with playing sounds in my iPhone app: I can manage to play simple sound effects to coincide with an event (in this instance, when the player or computer scores, or when the ball is hit), but I'm having trouble getting a soundtrack to play.
I'm using a .caf file (under instruction), and have placed it in my Resources folder. I've added the code:
Code:
SystemSoundID volleyFileID;
SystemSoundID gameStartFileID;
SystemSoundID soundTrackFileID;
in theViewController.h file, as well as:
Code:
@property(nonatomic) SystemSoundID volleyFileID;
@property(nonatomic) SystemSoundID gameStartFileID;
@property(nonatomic) SystemSoundID soundTrackFileID;
and placed the code:
Code:
#import <AudioToolbox/AudioServices.h>
@interface laserhockeyViewController : UIViewController {
IBOutlet UIImageView *puc;
IBOutlet UIImageView *paddleblue;
IBOutlet UIImageView *paddlegreen;
IBOutlet UILabel *tapToBegin;
IBOutlet UILabel *playerscore;
IBOutlet UILabel *computerscore;
CGPoint pucVelocity;
NSInteger gameState;
NSInteger playerscorevalue;
NSInteger computerscorevalue;
SystemSoundID volleyFileID;
SystemSoundID gameStartFileID;
SystemSoundID soundTrackFileID;
in the ViewController.m file. Everything seems to be in place, including the following code which I have placed underneath the viewDidLoad method:
Code:
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.gameState = kGameStatePaused;
pucVelocity = CGPointMake(kPucSpeedX,KPucSpeedY);
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
NSString *volleyPath = [[NSBundle mainBundle] pathForResource:@"puchit" ofType:@"caf"];
CFURLRef volleyURL = (CFURLRef ) [NSURL fileURLWithPath:volleyPath];
AudioServicesCreateSystemSoundID(volleyURL, &volleyFileID);
NSString *gamestartPath = [[NSBundle mainBundle] pathForResource:@"gamestart" ofType:@"caf"];
CFURLRef gamestartURL = (CFURLRef ) [NSURL fileURLWithPath:gamestartPath];
AudioServicesCreateSystemSoundID(gamestartURL, &gameStartFileID);
NSString *soundTrackPath = [[NSBundle mainBundle] pathForResource:@"Monolake - Cern" ofType:@"caf"];
CFURLRef soundTrackURL = (CFURLRef ) [NSURL fileURLWithPath:soundTrackPath];
AudioServicesCreateSystemSoundID(soundTrackURL, &soundTrackFileID);
}
Can anyone see what's wrong? The code compiles fine, and the build is successful, yet the sound doesn't play in the game, no matter where I place the "
AudioServicesPlaySystemSound(soundTrackID);" line of code.
Thanks in advance!