0

这是我现在录制的代码如何通过示例 UIlable 浮点数获取麦克风中的电平声音输入

我想我会使用这个功能,但我如何在 rec.h 和 rec.m 文件中使用它

(void)updateMeters

AVAudioSession * audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error:nil];
NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
recordedTmpName = [[NSString alloc] initWithFormat:@"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"aac"];
temporaryRecFile= [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:recordedTmpName]];
recorder = [[ AVAudioRecorder alloc] initWithURL:temporaryRecFile settings:recordSetting error:nil];
[recorder setDelegate:self];
[recorder prepareToRecord];
[recorder record];
4

1 回答 1

2

在录制过程中,您必须定期调用 updateMeters,并通过 - (float)averagePowerForChannel:(NSUInteger)channelNumber 获取平均功率

[recorder setDelegate:self];
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(handleTimer) userInfo:nil repeats:YES];




- (void) handleTimer
{
    [recorder updateMeters];
    label.text = [NSString stringWithFormat:@"lf",[recorder averagePowerForChannel:0]];
}

要查看更多信息,您可以访问AVAudioRecorder 类参考

于 2013-02-20T07:23:05.500 回答