开发者

How to display audio meter on an iPhone [duplicate]

This question already has an answer here: How to measure noise or sound and displays in dB(A) in iphone? (1 answer) Closed 9 years ago.

I have an audio recorder and I was wondering if anyone has a code to show a audio 开发者_Go百科meter (the bar that most of audio recorders have which shows the level of input audio).


Apple's SpeakHere example code includes a LevelView class which seems to be exactly what you are looking for.


Heres an example that calculates output and prints it:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURL *url = [NSURL fileURLWithPath:@"/dev/null"]; // Your audio save path

    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];
        levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
    } 
}


 - (void)levelTimerCallback:(NSTimer *)timer {
    [recorder updateMeters];

    const double ALPHA = 0.05;
    double peakPowerForChannel = pow(10, (0.05 * [recorder peakPowerForChannel:0]));
    lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;
    NSLog(@"%f",(lowPassResults*100.0f));
}

Depending on the lowPassResults you can animate a view accordingly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜