Why does averagePowerForChannel always return -160?

て烟熏妆下的殇ゞ 提交于 2019-12-20 03:09:33

问题


I have these code and both method call are SUCCESS.

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory: AVAudioSessionCategoryPlayAndRecord error: NULL];
[audioSession setActive: YES error: NULL];

And these code to start recording:

[self.recorder prepareToRecord];
[self.recorder recordForDuration: 60];

I have a timer function to update meters

- (void)updateMeters
{
    [self.recorder updateMeters];
    float peakPower = [self.recorder averagePowerForChannel: 0];
    double ALPHA = 0.05;
    double peakPowerForChannel = pow(10, (ALPHA * peakPower));
    self.recordView.progress = peakPowerForChannel;

    NSLog(@"%f", peakPower);
}

But I do not know why peakPower is always 160. I have another demo(from web) can work well, but I can not find any difference between them.


回答1:


Just try to set:

   recorder.meteringEnabled = YES;



回答2:


Set the category to AVAudioSessionCategoryPlayAndRecord and check

NSError *error;
[[AVAudioSession sharedInstance] 
    setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];

if (error) {
    NSLog(@"Error description: %@", [error description]);
}



回答3:


It is strange that [audioSession setCategory: AVAudioSessionCategoryPlayAndRecord error: &err] and [[AVAudioRecorder alloc] initWithURL: [NSURL fileURLWithPath: self.recordPath] settings: recordSetting error: NULL] must in the same function...It is the answer of this question.



来源:https://stackoverflow.com/questions/24991908/why-does-averagepowerforchannel-always-return-160

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