【发布时间】:2011-07-04 13:13:11
【问题描述】:
我正在使用 Cocos2D 和系统粒子,我希望我用英文正确地写了这个。 我在以某种方式识别 iPhone 麦克风的声音时遇到问题。
我的应用程序中有不同的部分,在其中一个部分中,我使用麦克风来检测是否有人向麦克风“吹气”。这部分一开始可以正常工作,但是如果您转到应用程序的其他播放声音的部分,然后您返回该区域并尝试吹气,它将无法正常工作。
我调试了代码,即使我不在这个场景中,levelTimeCallback 也一直在工作。我真的不知道发生了什么。我已经使用
停止了所有声音[[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
谁知道我做错了什么?顺便说一句,在模拟器中完美运行,但在 iPhone 中却不行。
记录器在onEnter方法中设置
-(void) onEnter {
[super onEnter];
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
NSError *error;
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if (recorder) {
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
NSLog(@"I'm in the recorder");
} else
NSLog(@"recorder error");
}
这是 levelTimerCallback 方法,如果声音被“检查”了
- (void)levelTimerCallback:(NSTimer *)timer {
[recorder updateMeters];
const double ALPHA = 0.05;
double peakPowerForChannel = pow(10, (0.05 * [recorder peakPowerForChannel:0]));
lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;
if (lowPassResults > 0.55)
{
NSLog(@"Mic blow detected");
self.emitter = [[CCParticleExplosion alloc] initWithTotalParticles:5];
[self addChild: emitter z:1];
emitter.texture = [[CCTextureCache sharedTextureCache] addImage: @"hoja.png"];
emitter.autoRemoveOnFinish = YES;
}
NSLog(@"Inside levelTimerCallback");}
【问题讨论】:
标签: audio cocos2d-iphone recorder