在 Objective-C 中监控当前 dB 电平
Monitor current dB level in Objective-C
我正在尝试通过 iPhone 麦克风显示传入声音的分贝级别,在模拟器中一切正常,但在实际的 iOS 设备上不起作用。好像 iPhone 没有检测到声音,这是我的代码:
- (void)viewDidLoad {
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
if (granted) {
[self setup];
NSLog(@"granted");
} else {
NSLog(@"denied");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Microphone Access Denied"
message:@"You must allow microphone access in Settings > Privacy > Microphone"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)setup {
// record audio to /dev/null
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
// some settings
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 2], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
// create a AVAudioRecorder
NSError *error;
self.recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
[self.recorder setMeteringEnabled:YES];
if (self.recorder) {
[self.recorder prepareToRecord];
self.recorder.meteringEnabled = YES;
[self.recorder record];
self.levelTimer = [NSTimer scheduledTimerWithTimeInterval:0.01f target:self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
[self.recorder updateMeters];
}
}
- (void)levelTimerCallback:(NSTimer *)timer {
[self.recorder updateMeters];
// here is the DB!
float peakDecebels = [self.recorder peakPowerForChannel:1];
NSLog(@"peak: %f", peakDecebels);
float avaeragePower = [self.recorder averagePowerForChannel:1];
NSLog(@"averagePower: %f", avaeragePower);
[self.dbLabel setText:[NSString stringWithFormat:@"db %f",peakDecebels]];
}
您需要适当地设置您的音频会话:
NSError* error = nil;
BOOL success = [[AVAudioSession sharedInstance]
setCategory:AVAudioSessionCategoryRecord
error:&error];
if (!success && error) {
NSLog(@"error %@",error);
}
这应该在您开始录制之前完成。
我正在尝试通过 iPhone 麦克风显示传入声音的分贝级别,在模拟器中一切正常,但在实际的 iOS 设备上不起作用。好像 iPhone 没有检测到声音,这是我的代码:
- (void)viewDidLoad {
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
if (granted) {
[self setup];
NSLog(@"granted");
} else {
NSLog(@"denied");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Microphone Access Denied"
message:@"You must allow microphone access in Settings > Privacy > Microphone"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)setup {
// record audio to /dev/null
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
// some settings
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 2], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
// create a AVAudioRecorder
NSError *error;
self.recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
[self.recorder setMeteringEnabled:YES];
if (self.recorder) {
[self.recorder prepareToRecord];
self.recorder.meteringEnabled = YES;
[self.recorder record];
self.levelTimer = [NSTimer scheduledTimerWithTimeInterval:0.01f target:self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
[self.recorder updateMeters];
}
}
- (void)levelTimerCallback:(NSTimer *)timer {
[self.recorder updateMeters];
// here is the DB!
float peakDecebels = [self.recorder peakPowerForChannel:1];
NSLog(@"peak: %f", peakDecebels);
float avaeragePower = [self.recorder averagePowerForChannel:1];
NSLog(@"averagePower: %f", avaeragePower);
[self.dbLabel setText:[NSString stringWithFormat:@"db %f",peakDecebels]];
}
您需要适当地设置您的音频会话:
NSError* error = nil;
BOOL success = [[AVAudioSession sharedInstance]
setCategory:AVAudioSessionCategoryRecord
error:&error];
if (!success && error) {
NSLog(@"error %@",error);
}
这应该在您开始录制之前完成。