- (void)viewDidLoad {
[superviewDidLoad];
NSURL *url = [NSURLfileURLWithPath:@"/dev/null"];
NSDictionary *settings = [NSDictionarydictionaryWithObjectsAndKeys:
[NSNumbernumberWithFloat: 44100.0], AVSampleRateKey,
[NSNumbernumberWithInt: kAudioFormatAppleLossless],AVFormatIDKey,
[NSNumbernumberWithInt: 1], AVNumberOfChannelsKey,
[NSNumbernumberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
NSError *error;
recorder = [[AVAudioRecorderalloc] initWithURL:urlsettings:settings error:&error];
if (recorder) {
[recorderprepareToRecord];
recorder.meteringEnabled =YES;
[recorderrecord];
levelTimer = [NSTimerscheduledTimerWithTimeInterval: 0.03 target:self selector: @selector(levelTimerCallback:)userInfo: nilrepeats: YES];
}
}
levelTimerCallback函数是这么写的
- (void)levelTimerCallback:(NSTimer *)timer {
[recorderupdateMeters];
const double ALPHA = 0.05;
double peakPowerForChannel = pow(10, (0.05 * [recorderpeakPowerForChannel:0]));
lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;
if (lowPassResults < 0.95){
NSLog(@"低音状态");
}
else {
NSLog(@"高音状态");
}
}
demo链接demo 中看运行下面的输出