大家好我有问题..
如果是那种类型的字母(从标签中逐个字母),我基本上有一种方法可以将您与 3 进行比较。(如果是点、线或空格)。根据类型启动再现特定声音的代码。我遇到的问题是,启动此方法后,应用程序会冻结,直到完成 cycleFor。
我想知道,有没有办法让他在后台或另一个线程中做?对我来说,用户可以在此代码进行时执行其他操作就足够了。
我什至不能用另一个按钮停止播放器,我该怎么办?
我声明我是一个新手,我几乎不知道什么是 thred
提前感谢您的帮助,对不起英语
这是我的代码:
-(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];
}
}
}