Note that there are some explanatory texts on larger screens.

plurals
  1. POAVAudioRecorder averagePowerForChannel always returns -120.0
    text
    copied!<p>I'm trying to use AVAudioRecorder's averagePowerForChannel method to monitor input levels on the microphone for an iPad/iPhone app. I have a callback which polls the average level in a loop — on the iPhone it works fine and returns sensible levels, but for some reason on the iPad it always returns -120.0. </p> <p>Here's some of my setup code:</p> <pre><code>- (void) setupMic { if (micInput) { [micInput release]; micInput = nil; } NSURL *newURL = [[NSURL alloc] initFileURLWithPath:@"/dev/null"]; NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init]; [recordSettings setObject:[NSNumber numberWithInt:kAudioFormatAppleLossless] forKey: AVFormatIDKey]; [recordSettings setObject:[NSNumber numberWithFloat:22050.0] forKey: AVSampleRateKey]; // [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey]; [recordSettings setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey]; [recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; [recordSettings setObject:[NSNumber numberWithInt: AVAudioQualityLow] forKey: AVEncoderAudioQualityKey]; micInput = [[AVAudioRecorder alloc] initWithURL:newURL settings:recordSettings error:nil]; // [micInput setMeteringEnabled:YES]; [newURL release]; [recordSettings removeAllObjects]; [recordSettings release]; } </code></pre> <p>As well as my start recording method:</p> <pre><code>- (void) startRecording { NSLog(@"startRecording!"); [micInput pause]; [micInput prepareToRecord]; micInput.meteringEnabled = YES; [micInput record]; [micInput updateMeters]; levelTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:0.0] interval:0.03 target:self selector:@selector(levelTimerCallback:) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:levelTimer forMode:NSDefaultRunLoopMode]; } </code></pre> <p>and a bit of the levelTimer callback:</p> <pre><code>- (void)levelTimerCallback:(NSTimer *)timer { [micInput updateMeters]; double avgPowerForChannel = pow(10, (0.05 * [micInput averagePowerForChannel:0])); [micSprite receiveInput:avgPowerForChannel]; NSLog(@"Avg. Power: %f", [micInput averagePowerForChannel:0]); ... } </code></pre> <p>Where on the iPhone, the NSLog statement will return sensible values, and the iPad will always return -120.0. </p> <p>Note: I'm using this inside of a cocos2d application. For some reason, if I restart the current scene on the iPad, the mic levels will return correct values.</p> <p>Anyone have any suggestions? I'm seriously at a loss here. Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload