Quote:
Originally Posted by tackey123
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.