4

I have an audio file that I want to begin playing at a 20 second delay, after the user has seen some animations play etc...

Does anyone know how I might go about doing this? I have 5 animations that play, after which I would like audio file to begin. The whole thing would keep cycling until the user exits the app.

Here's the code I have that plays the audio file. I can get it to play if a button is pressed or on viewDidLoad, that works fine.

NSString *audioSoundPath = [[ NSBundle mainBundle] pathForResource:@"audio_file" ofType:@"caf"];
CFURLRef audioURL = (CFURLRef) [NSURL fileURLWithPath:audioSoundPath];
AudioServicesCreateSystemSoundID(audioURL, &audioID); 

AudioServicesPlaySystemSound(audioID);

thanks for any help

jv42
  • 8,521
  • 5
  • 40
  • 64
hanumanDev
  • 6,592
  • 11
  • 82
  • 146

2 Answers2

3

If you know its always going to be exactly 20 seconds then you can use an NSTimer to call a method that starts the audio:

[NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(startPlaying) userInfo:nil repeats:NO];


-(void)startPlaying {

    NSString *audioSoundPath = [[ NSBundle mainBundle] pathForResource:@"audio_file" ofType:@"caf"];
    CFURLRef audioURL = (CFURLRef) [NSURL fileURLWithPath:audioSoundPath];
    AudioServicesCreateSystemSoundID(audioURL, &audioID);
    AudioServicesPlaySystemSound(audioID);
}
Ben
  • 2,982
  • 1
  • 16
  • 12
2

You can also use

[self performSelector: @selector(playSound:) withObject: audioURL afterDelay: 20.0f];

jv42
  • 8,521
  • 5
  • 40
  • 64
  • Of course you can choose to pass anything you want as parameter, as long as it can be cast to `id`. – jv42 Oct 08 '10 at 14:52