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-26-2010, 05:21 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2009
Age: 15
Posts: 93
tackey123 is on a distinguished road
Question Is there is something similar to sleep() ?

Here is the code;

Code:
-(void)loop1 {
	
	int x = 0;
	while (x <= slider.value ) {
	
	NSString *path = [[NSBundle mainBundle] pathForResource:@"SOMNIUM1_3" ofType:@"aif"];
	AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
	theAudio.delegate = self;
	[theAudio play];
	[NSThread sleepForTimeInterval:160];
	x++;	
		
	}
	

	
}
what I am trying to do is just wait for 160 seconds then do x++; ... But the problem is, when the sleep command is called it stops the whole thread which makes everything freeze so I can't make any buttons or anything. So is there anyway I could just block the code for 160 seconds without freezing the main thread.
Thanks in advanced!
tackey123 is offline   Reply With Quote
Old 05-26-2010, 05:50 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

NSTimer

performSelector:withObject:afterDelay:

Edit: Oh, misunderstood the goal. Implement the delegate method, then wait for the audio to finish. (I assume the 160 corresponds to the length of the sounds)
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 05-26-2010, 06:02 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2009
Age: 15
Posts: 93
tackey123 is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
NSTimer

performSelector:withObject:afterDelay:

Edit: Oh, misunderstood the goal. Implement the delegate method, then wait for the audio to finish. (I assume the 160 corresponds to the length of the sounds)
No that won't work, 160 is half way through. Thats why I need it to be blocked for awhile.
tackey123 is offline   Reply With Quote
Old 05-26-2010, 06:55 PM   #4 (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
Here is the code;

Code:
-(void)loop1 {
	
	int x = 0;
	while (x <= slider.value ) {
	
	NSString *path = [[NSBundle mainBundle] pathForResource:@"SOMNIUM1_3" ofType:@"aif"];
	AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
	theAudio.delegate = self;
	[theAudio play];
	[NSThread sleepForTimeInterval:160];
	x++;	
		
	}
	

	
}
what I am trying to do is just wait for 160 seconds then do x++; ... But the problem is, when the sleep command is called it stops the whole thread which makes everything freeze so I can't make any buttons or anything. So is there anyway I could just block the code for 160 seconds without freezing the main thread.
Thanks in advanced!

It sounds like you don't want to put a thread to sleep. Instead, you want to repeat an action every 160 seconds.

I would suggest creating a repeating timer that fires every 160 seconds. Use the call scheduledTimerWithTimeInterval:target:selector:use rInfo:repeats:


Something like this:
Code:
//In header
NSInteger count;
NSTimer myTimer*;
AVAudioPlayer* theAudio;



//In .m file (when you want to activate the timer)
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio prepareToPlay];
myTimer = [NSTimer scheduledTimerWithTimeInterval: 160
                              target:    self
                              selector: @selector(playSound)
                              userInfo: nil
                              repeats:  yes ];

- (void) playSound;
{
	if (count++ > slider.value) 
	{
		[myTimer invalidate];
		theAudio = nil;
	}
	else
	{
		theAudio.delegate = self;
		[theAudio play];
	}
}

Note that your code was creating a separate instance of AVAudioPlayer each time you played the sound, and not releasing it. If you are playing the same sound each time, you should create a single instance and reuse it. You also need to release it when you are done with it. If you're only creating a single audio player, you can always release it in your class's dealloc method.
Duncan C is offline   Reply With Quote
Old 05-26-2010, 07:04 PM   #5 (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
No that won't work, 160 is half way through. Thats why I need it to be blocked for awhile.


What DO you want to do? Play a sound every 160 seconds? Play a sound over and over, as soon as it finishes? Play it over and over with a short delay between?

As the other poster said, adding the delegate method audioPlayerDidFinishPlaying:successfully: would be a good way to wait until the sound finishes playing before playing it again. That method gets called when the sound finishes playing. You could then invoke the sound again immediately, or use performSelector:withObject:afterDelay to invoke the sound after a delay.


Regards,

Duncan Champney
Wareto
Check out our apps in the Apple App store
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
freezing, help me, nstimer, sleep()

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: 316
10 members and 306 guests
ajay123123, ashaman64, baja_yu, ChrisYates, guusleijsten, HemiMG, newDev, Objective Zero, pkIDSF, 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,878
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:53 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0