使用AVAudioPlayer在后台播放音频

时间:2014-02-19 14:50:07

标签: objective-c multithreading avaudioplayer morse-code

大家好我有问题..

我基本上有一种方法可以将你与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];

        }
    }

}

1 个答案:

答案 0 :(得分: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(只是一个例子,你可以用其他方式处理它)