Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-01-2010, 06:52 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 19
Ubermatik is on a distinguished road
Default Adding a soundtrack to an iPhone game.

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!
Ubermatik is offline   Reply With Quote
Old 08-01-2010, 07:00 AM   #2 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 19
Ubermatik is on a distinguished road
Default

I should have added that I want the music to play as soon as the game loads.
Ubermatik is offline   Reply With Quote
Old 08-01-2010, 10:12 AM   #3 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 19
Ubermatik is on a distinguished road
Default

*Bump

Could anyone help me? I'm sure it's only simple, but I just can't get it...

Thanks.
Ubermatik is offline   Reply With Quote
Old 08-01-2010, 10:46 AM   #4 (permalink)
iPhone SDK fanatics!
 
Join Date: Aug 2009
Location: Malaysia
Posts: 405
KennyChong is on a distinguished road
Default

Quote:
Originally Posted by Ubermatik View Post
*Bump

Could anyone help me? I'm sure it's only simple, but I just can't get it...

Thanks.


Try this:
add AVFoundation.framework


#import "AudioToolbox/AudioToolbox.h"



if (!backgroundMusic.playing) {
NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource: @"music_back1" ofType: @"m4a"];
NSURL *backgroundMusicURL = [[NSURL alloc] initFileURLWithPath:backgroundMusicPath];
backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:nil];
[backgroundMusic play];
backgroundMusic.numberOfLoops = -1;
backgroundMusic.volume = 0.1;
}
__________________
KennyChong
iPhone SDK Fanatic!
KennyChong is offline   Reply With Quote
Old 08-01-2010, 10:56 AM   #5 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,586
JasonR is on a distinguished road
Default

AudioServicesPlaySystemSound is only designed to play sounds of 30 seconds or less, which is probably the problem. KennyChong's solution can play songs of any length and format.
JasonR is offline   Reply With Quote
Old 08-01-2010, 12:14 PM   #6 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 19
Ubermatik is on a distinguished road
Default

I'm getting the error "Request for member ' ' in something not a structure or union" - have I placed the code in the wrong part? I placed it in the -(void)viewDidLoad method...

I am also getting "AVAudioPlayer undeclared"

Any ideas? Thanks.
Ubermatik is offline   Reply With Quote
Old 08-01-2010, 12:30 PM   #7 (permalink)
iPhone SDK fanatics!
 
Join Date: Aug 2009
Location: Malaysia
Posts: 405
KennyChong is on a distinguished road
Default

Quote:
Originally Posted by Ubermatik View Post
I'm getting the error "Request for member ' ' in something not a structure or union" - have I placed the code in the wrong part? I placed it in the -(void)viewDidLoad method...

I am also getting "AVAudioPlayer undeclared"

Any ideas? Thanks.
Did you add the Audio framework first?
__________________
KennyChong
iPhone SDK Fanatic!
KennyChong is offline   Reply With Quote
Old 08-01-2010, 02:33 PM   #8 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 19
Ubermatik is on a distinguished road
Default

Yep... it all seems to be there... I'll check over once more though and get back to you...
Ubermatik is offline   Reply With Quote
Old 08-01-2010, 03:20 PM   #9 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 19
Ubermatik is on a distinguished road
Default

Do I need to have another .h file, and reference it in my .m file? Because I can't see whats wrong...

Last edited by Ubermatik; 08-01-2010 at 03:20 PM. Reason: incorrect spelling
Ubermatik is offline   Reply With Quote
Old 08-01-2010, 09:39 PM   #10 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,586
JasonR is on a distinguished road
Default

I added this line myself:
Code:
#import <AVFoundation/AVFoundation.h>
JasonR is offline   Reply With Quote
Old 08-02-2010, 08:00 AM   #11 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 19
Ubermatik is on a distinguished road
Default

Yeah, I have that in too... is the problem that in my .h file, I have written it as SystemSound (as you would for AudioServicesSystemSound) instead of something else...?
Ubermatik is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 352
9 members and 343 guests
alexP, gordo26, headkaze, mistergreen2011, nobstudio, Objective Zero, rayjeong, revg, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:16 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0