1

I am listing from the mic using AVAudioEngine by this code:

 NSError *err2 = nil;

engine = [[AVAudioEngine alloc] init];
mainMixer = [engine mainMixerNode];
[mainMixer installTapOnBus:0 bufferSize:1024 format:[mainMixer outputFormatForBus:0] block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {
    [self normalize:buffer];
}];
[engine startAndReturnError:&err2];

Then as u see passing the buffer I get into a function called normalize:

-(void) normalize:(AVAudioPCMBuffer*) buffer{
float sum = 0;

NSMutableArray *values = [NSMutableArray new];

for (AVAudioFrameCount i = 0; i < buffer.frameLength; i++) {
    [values addObject:@(buffer.floatChannelData[0][i])];
}

for (int i = 0 ; i< [values count]; i++){
    if ([values[i] floatValue] != 0){
        NSLog(@"%f", [values[i] floatValue]) ;
    }
}
}

for now, I only want to print the values of floatChannelData. The problem is that it is always printing 0. so the buffer has all values 0. Why is this happening? shouldn't the values be changing with the change of voices received by the mic?

Cœur
  • 37,241
  • 25
  • 195
  • 267
mahdi
  • 149
  • 12

1 Answers1

0

This solved it for me:

  • Go to your "Info.plist" file.
  • Right-click and click "Add Row".
  • Then, in the first column of the newly created row, find the key "Privacy - Microphone Usage Description" and add a suitable explanation of what you intend to do with the audio as the key's value (third column).

Now a dialog box will appear when you run your app asking for permission to access the microphone. Basically, you can't retrieve audio unless you have explicit permission from the user, even if you can technically talk to the audio device and start the audio engine.