Reputation: 71
I would like to ask about the problem with a simple microphone volume level detection. My code works just fine with iOS 6 or lower but not iOS 7, my code looks like this:
-(void)viewDidLoad{
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];
} else{
NSLog([error description]);
}
}
// then call periodically with the following method, volumeLevelSampling
-(void)volumeLevelSampling{
[recorder updateMeters];
NSLog(@"Average input: %f Peak input: %f", [recorder averagePowerForChannel:0], [recorder peakPowerForChannel:0]);
}
It works perfectly fine in iOS 6, however it's not sampling anything in iOS 7. The result is always -120.
Upvotes: 7
Views: 5811
Reputation: 26
I was having the same problem and here is how I was able to make it work:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error:nil];
if([audioSession respondsToSelector:@selector(requestRecordPermission:)]){
//ios7
[audioSession requestRecordPermission:^(BOOL granted) {
if(granted){
[self recordNow];
}else{
NSLog(@"RECORD NOT AUTHORIZED");
}
}];
}else{
//before ios7
[self recordNow];
}
Upvotes: 0
Reputation: 4515
Check in Settings if you have activated the permissions for your application. This works like the push notifications.
Once the alert is answered it wont pop up again. You have to go to:
Settings -> Privacy -> Microphone
Then check the state of your app.
If you can't see your app there, it means that you are not correctly requesting access to the microphone. Try this.
if([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission:)]) {
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
if (!granted) {
NSLog(@"User will not be able to use the microphone!");
}
}];
}
Hope it helps as a clue.
Cheers!
Upvotes: 6
Reputation: 31304
In iOS 7 users must provide consent to apps that access the microphone.
Before you can use AVAudioRecorder
you must first request this consent. This is done using the requestAccessForMediaType:completionHandler:
method on AVCaptureDevice
.
If you don't have consent - or you haven't requested it - you'll get silence when trying to record anything on iOS 7.
Upvotes: 3