Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 使用AVAudioPlayer在后台播放音频_Objective C_Multithreading_Avaudioplayer_Morse Code - Fatal编程技术网

Objective c 使用AVAudioPlayer在后台播放音频

Objective c 使用AVAudioPlayer在后台播放音频,objective-c,multithreading,avaudioplayer,morse-code,Objective C,Multithreading,Avaudioplayer,Morse Code,大家好,我有个问题 我有一个基本的方法,将你和3进行比较,如果那种类型的字母是从标签上取下来的。如果是点、线或空间。 根据类型,启动一个代码以再现特定的声音。 我遇到的问题是,启动此方法后,应用程序会冻结,直到完成cycleFor 我想知道,有没有办法让他在后台或其他线程中完成?对于我来说,用户可以在执行此代码时执行其他操作就足够了 我甚至不能用另一个按钮停止播放器,我怎么办 我声明我是一个新手,我几乎不知道什么是thred 提前谢谢你的帮助,对不起英语 这是我的代码: -(IBAction)t

大家好,我有个问题

我有一个基本的方法,将你和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];

        }
    }

}

应用程序冻结,因为您要求应用程序冻结。问题是:

    [player play];
    NSLog(@"beepCorto");
    sleep(1);
    //aspect 1 seconds because if I don't, you hear nothing
    [player stop];
你每次都让应用程序睡眠1秒。这是非常糟糕的,你的应用程序可能会失去响应并崩溃

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

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

1.-让你的班级遵守协议。要执行此操作,请修改声明

@interface MyClass : NSObject <AVAudioPlayerDelegate>
3.-每次创建AVAudioPlayer对象时,都会将其委托给self,就像您已经在做的那样


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

谢谢,我不知道这是一件坏事。。谢谢你提供的信息。还有一个问题,我不知道如何开始使用代理AVAudioPlayers。对不起,我还是个新手。。你能给我一些帮助吗?我理解代码,问题是一旦开始循环就不会停止,直到它完成。我想知道你是否可以用另一个按钮停止正在进行的循环。
(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
}