1
votes

I have an app containing three buttons , play , pause and stop . In viewDidLoad I do the following :

 NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:songName ofType:@"mp3"];
 NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
 player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];

[playBtn addTarget:self action:@selector(playSong)   forControlEvents:UIControlEventTouchUpInside];
[pauseBtn addTarget:self action:@selector(pauseSong) forControlEvents:UIControlEventTouchUpInside];
[stopBtn addTarget:self action:@selector(stopSong) forControlEvents:UIControlEventTouchUpInside];

the playSong function is :

-(void) playSong
{
    [player play];
}

If I press the play button it stays pressed for about 3 seconds while the file is loaded and then the music starts playing . Is there a way to make the background music play instantly as I press the play button ?

1

1 Answers

5
votes

Call [player prepareToPlay]; earlier to preload the clip.