Saturday, 24 August 2013

Unable to play mp3 file in iPhone game

Unable to play mp3 file in iPhone game

I want to play background music in my game (ideally i'd like it to fade in
/ out but I want it playing first)
I have looked at various answers on SO and my code seems to look correct
but for some reason the file isnt playing, in fact it crashes (with no
useful output) on the AVAudioPlayer *player line. If I continue it crashes
again on the [player play] line but error is 0.
Updated:
GameViewController.h
@interface GameViewController : UIViewController{
AVAudioPlayer* player;
}
GameViewController.m
NSString *bgsoundFilePath = [[NSBundle mainBundle]
pathForResource:@"bgtrack" ofType:@"mp3"];
NSURL *bgsoundFileURL = [NSURL fileURLWithPath:bgsoundFilePath];
NSError *error;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:bgsoundFileURL
error:&error];
player.numberOfLoops = -1; //infinite
player.currentTime = 139;// 2:19;
[player play];
I am including
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>

No comments:

Post a Comment