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 05-19-2010, 06:15 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2009
Age: 15
Posts: 93
tackey123 is on a distinguished road
Default How to pause before running another piece of code

I am Trying to play an audio file then wait 2 seconds then go to a method. Right now I am using the sleep command but that stops the whole thread which makes the app crash. So I was wondering if anybody knew how to pause for a little while then go to a method.

here is where the audio and sleep are;
Code:
-(void)loop2 {
	NSString *path = [[NSBundle mainBundle] pathForResource:@"LOOP2" ofType:@"aif"];
	AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
	theAudio.delegate = self;
	[theAudio play];
	
    sleep(2);
	[self random];
	
}
And here is the method;

Code:
-(void)random {
//do something
}
PLEASE HELP! Thanks!
tackey123 is offline   Reply With Quote
Old 05-19-2010, 07:16 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

Well, you're right; you shouldn't be calling sleep. But that won't crash your iPhone app.

I would guess your crash is somewhere in your -random method, although it could be due to you not using AVAudioPlayer correctly... because what you describe as your intended program flow is not at all what the code you posted will do. AVAudioPlayer's -play method doesn't block; it returns immediately.
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 05-19-2010, 07:45 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by tackey123 View Post
I am Trying to play an audio file then wait 2 seconds then go to a method. Right now I am using the sleep command but that stops the whole thread which makes the app crash. So I was wondering if anybody knew how to pause for a little while then go to a method.

here is where the audio and sleep are;
Code:
-(void)loop2 {
	NSString *path = [[NSBundle mainBundle] pathForResource:@"LOOP2" ofType:@"aif"];
	AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
	theAudio.delegate = self;
	[theAudio play];
	
    sleep(2);
	[self random];
	
}
And here is the method;

Code:
-(void)random {
//do something
}
PLEASE HELP! Thanks!

As the other poster said, the AVAudioPlayer class returns immediately when you start it playing.

You want to implement the method audioPlayerDidFinishPlaying:successfully: in your AVAudioPlayer delegate. That method will get called when the sound is finished playing.

In your audioPlayerDidFinishPlaying:successfully: method, you can use the method performSelector:withObject:afterDelay: to tell your control object to play the next sound. The performSelector:withObject:afterDelay: call returns immediately, and starts a timer that causes your method to fire after the specified time-span has elapsed. It saves you the work of creating a timer yourself.



Regards,

Duncan C
WareTo
Duncan C is offline   Reply With Quote
Old 05-19-2010, 08:44 PM   #4 (permalink)
Elegance is Infinite
iPhone Dev SDK Supporter
 
TapTouchClick's Avatar
 
Join Date: Jan 2010
Location: Bay Area, CA
Posts: 677
TapTouchClick is on a distinguished road
Send a message via AIM to TapTouchClick Send a message via Skype™ to TapTouchClick
Default

an example:

Code:
[self performSelector:@selector(random) withObject:nil afterDelay:2];
HTH
__________________
Our website
TapTouchClick is offline   Reply With Quote
Old 05-20-2010, 09:42 AM   #5 (permalink)
Registered Member
 
Join Date: Sep 2009
Age: 15
Posts: 93
tackey123 is on a distinguished road
Default

Thanks guys!

This is what worked for me;

Code:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:24 target:self selector:@selector(random) userInfo:nil repeats:NO];
tackey123 is offline   Reply With Quote
Reply

Bookmarks

Tags
method, nstimer, sleep, wait

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: 307
9 members and 298 guests
Abidullah, ajay123123, Fstuff, guusleijsten, HemiMG, newDev, pkIDSF, Sami Gh, Steven.C
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,648
Threads: 94,113
Posts: 402,877
Top Poster: BrianSlick (7,990)
Welcome to our newest member, brandon6031
Powered by vBadvanced CMPS v3.1.0

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