0

大家好我有问题..

如果是那种类型的字母(从标签中逐个字母),我基本上有一种方法可以将您与 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];

        }
    }

}

4

1 回答 1

0

该应用程序冻结是因为您要求该应用程序冻结。问题是这样的:

    [player play];
    NSLog(@"beepCorto");
    sleep(1);
    //aspect 1 seconds because if I don't, you hear nothing
    [player stop];

您是在告诉应用程序每次睡眠 1 秒钟。这非常糟糕,您的应用程序可能会变得无响应并崩溃。

您需要完全重写您的函数以防止应用程序冻结,使用 AVAudioPlayer 的委托......或者只是在不同的线程上运行您的代码。

编辑:要实现委托,请执行以下操作:

1.- 让你的类符合AVAudioPlayerDelegate协议。为此,您需要修改声明。

@interface MyClass : NSObject <AVAudioPlayerDelegate>

2.-让你的类实现协议的audioPlayerDidFinishPlaying:successfully:方法。

(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    // This function will be called when the 'player' has finished playing
    beepCount ++
    NSString *ch = [code substringWithRange:NSMakeRange(beepCount, 1)];
    // ...
    // Here you can instantiate the next AVAudioPlayer to play the next beep
    // Don't forget to set the delegate again
}

3.- 每次创建一个 AVAudioPlayer 对象时,它都会被委托给它self(正如你已经在做的那样)

你需要在你的类中保留一个额外的变量来跟踪一直在播放的哔哔声。在我上面的例子中,这将是变量 beepCount (只是一个例子,你可以用其他方式处理它)

于 2014-02-19T14:58:14.757 回答