objective-c - 在 Objective-C 中监控当前的 dB 级别

标签 objective-c microphone avaudiorecorder avaudiosession

我试图通过 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]];
}

最佳答案

您需要适本地设置 Audio Session :

NSError* error = nil;
   BOOL success = [[AVAudioSession sharedInstance]     
                            setCategory:AVAudioSessionCategoryRecord 
                                  error:&error];
    if (!success && error) {
        NSLog(@"error %@",error);
    }

这应该在开始录制之前完成。

关于objective-c - 在 Objective-C 中监控当前的 dB 级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28448613/

相关文章:

ios - UICollectionView Controller 中的自定义单元格未被调用

ios - 使用钩子(Hook)在 Instagram 上发布视频

iphone - iPhone 上的 Core Audio - 有什么方法可以改变麦克风增益(扬声器麦克风或耳机麦克风)?

c# - 从麦克风捕获音频而不回放

ios - 为什么用swift录制后听不到音乐?

java - 从文件中读取 int[][]。 java

ios - objective-c 中具有双 y 轴的多个条形图

在 Unity 游戏中使用麦克风时 iOS 扬声器音量降低

ios - 如何录制speex格式的音频文件?

ios - AVAudio Recorder 不在后台录音