How to display audio meter on an iPhone [duplicate]

淺唱寂寞╮ 提交于 2019-12-07 18:46:21

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/5686948/how-to-display-audio-meter-on-an-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!