Hello guys I have a problem ..
I have basically a method that compares you with 3 if that type of letter (letter by letter taken from a label). (If a point, a line or a space). depending on the type launches a code that reproduces a specific sound. the problem I have is that, launched this method, the app freezes until it has finished cycleFor.
I wonder, is there a way to get him to do in the background or in another thread? is enough for me that the user can do something else while is in progress this code.
I can not even with another button that stops the player, how could I do?
I state that I am a novice and I know scarcely what are the thred
Thanks in advance for your help, sorry for the English
This is my code:
-(IBAction)transmitSound:(id)sender {
NSString *code = self.labelCode.text;
//labelCode is the label that contains the code to be translated
for (int i = 0; i <= [code length]; i++) {
NSString * ch = [code substringWithRange:NSMakeRange(i, 1)];
//I need to compare a letter at a time
if ([ch isEqual:nil]) {nil;}
//should avoid error .. or is it useless?
if ([ch isEqual: @"."]) {
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/beepCorto.mp3"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:resourcePath] error:nil];
[player setDelegate:self];
[player play];
NSLog(@"beepCorto");
sleep(1);
//aspect 1 seconds because if I don't, you hear nothing
[player stop];
}
else if ([ch isEqual: @"-"]) {
if (player) {
if ([player isPlaying]) {
[player stop]; } }
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/beepLungo.mp3"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:resourcePath] error:nil];
[player setDelegate:self];
[player play];
NSLog(@"beepLungo");
sleep(1);
[player stop];
}
//se trovi un " " (spazio)
else if ([ch isEqual: @" "]) {
if (player) {
if ([player isPlaying]) {
[player stop]; } }
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/silenzio.mp3"];
player =[ [AVAudioPlayer alloc]initWithContentsOfURL: [NSURL fileURLWithPath:resourcePath] error:nil];
[player setDelegate:self];
[player play];
NSLog(@"silenzio");
sleep(1);
[player stop];
}
}
}