2

我对这段代码感到恼火。我试图在录制音频时获取averagePowerForChannel 和 peakPowerForChannel,但每次我将其设为 0.0

下面是我录制音频的代码:

 NSMutableDictionary *recordSetting =[[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithFloat: 22050.0], AVSampleRateKey,
            [NSNumber numberWithInt: kAudioFormatLinearPCM], AVFormatIDKey,
            [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
            [NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
            [NSNumber numberWithInt:32],AVLinearPCMBitDepthKey,
            [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
            [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
            nil];

recorder1 = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:audioFilePath] settings:recordSetting error:&err];
 recorder1.meteringEnabled = YES;
 recorder1.delegate=self;
 [recorder1 prepareToRecord];
 [recorder1 record];
 levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.3f target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];

- (void)levelTimerCallback:(NSTimer *)timer {

 [recorder1 updateMeters];

 NSLog(@"Peak Power : %f , %f", [recorder1 peakPowerForChannel:0], [recorder1 peakPowerForChannel:1]);
 NSLog(@"Average Power : %f , %f", [recorder1 averagePowerForChannel:0], [recorder1 averagePowerForChannel:1]);

}

代码中的错误是什么???

4

2 回答 2

6

我认为您必须在prepareToRecord 之后使用属性meteringEnabled。只需注释您将通道数指定为 1 的部分。让它使用默认值。希望能帮助到你。

于 2010-09-16T12:47:20.273 回答
2

我认为如果您将 AVNumberOfChannelsKey 设置为 2 ...它将起作用。

于 2010-06-28T19:41:18.187 回答