开发者

playing an audio file after a time delay

开发者 https://www.devze.com 2023-01-19 18:49 出处:网络
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...

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


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);
}


You can also use

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

0

精彩评论

暂无评论...
验证码 换一张
取 消