如何在iPhone上显示音频表

时间:2011-04-16 13:54:58

标签: iphone objective-c avaudiorecorder

我有一台录音机,我想知道是否有人有代码显示音频表(大多数录音机都有显示输入音频电平的条形码)。

2 个答案:

答案 0 :(得分:4)

Apple的SpeakHere示例代码包含一个LevelView类,它似乎正是您正在寻找的。

答案 1 :(得分:2)

下面是一个计算输出并打印出来的例子:

- (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));
}

根据 lowPassResults ,您可以相应地为视图设置动画。