0

我有一个在应用程序启动时打开的主视图(或 cocos2d 中的场景)。从那里我调用 audioUnit 来收听来自麦克风的输入音频。它工作得很好(audioUnit 上的回调总是被调用并做一些事情),但我可以看到它占用了主线程——这是我不想要的,因为主场景UILabel和其他需要成为主线程的东西才能被改变。

main scene我开始时:

remoteIns=[[remoteIO alloc]init]; //turn on the callback function on class remoteIO .
[remoteIns StartListeningWithFrequency]; // this function takes the main thread

然后当我main scene稍后签入时:

NSLog(@"isMainThread%d",[NSThread isMainThread]);

我可以看到它不是主线程,因此我无法更改UILabels它。

我需要启动audioUnit函数(另一个类)并将其设置在另一个线程中,保持主场景为主线程,并让audioUnit工作正常。

4

2 回答 2

2

在主线程上运行某些东西的一种更简单的方法是使用块。这样,您可以执行多个操作,并且看起来像“正常”代码。调用带有两个以上参数的例程也简单得多:

dispatch_sync(dispatch_get_main_queue(), ^{
    // Any code that you put in here will be run on the main thread.
    // However, MAKE SURE that you do not actually call this from the main thread or
    // your program will hang!

    // i.e.:
    self.label.text = @"New text";       
});
于 2012-11-28T00:14:26.247 回答
0

音频单元不在主线程中。但是主场景也不在主线程中。

为了改变主场景上的 UILabel ,它主要是主线程,所以我用这个来改变它的文本:(它在场景观察者中被调用来调用一个改变文本的函数:

[self performSelectorOnMainThread: @selector( updateText: ) withObject: bits waitUntilDone: YES];

并在updateText:我更改文本。效果很好。

于 2012-11-27T10:31:41.380 回答