2011-02-16 64 views
8

我試圖使用AVAudioRecorder的averagePowerForChannel方法來監視iPad/iPhone應用程序的麥克風輸入級別。我有一個回調輪詢平均水平 - 在iPhone上它工作正常,並返回合理的水平,但由於某種原因在iPad上它總是返回-120.0。AVAudioRecorder averagePowerForChannel總是返回-120.0

下面是我的一些設置代碼:

- (void) setupMic { 
if (micInput) { 
    [micInput release]; 
    micInput = nil; 
} 
NSURL *newURL = [[NSURL alloc] initFileURLWithPath:@"/dev/null"]; 

NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init]; 

[recordSettings setObject:[NSNumber numberWithInt:kAudioFormatAppleLossless] forKey: AVFormatIDKey]; 
[recordSettings setObject:[NSNumber numberWithFloat:22050.0] forKey: AVSampleRateKey]; 
// [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey]; 
[recordSettings setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey]; 
[recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; 
[recordSettings setObject:[NSNumber numberWithInt: AVAudioQualityLow] forKey: AVEncoderAudioQualityKey]; 

micInput = [[AVAudioRecorder alloc] initWithURL:newURL settings:recordSettings error:nil]; 
// [micInput setMeteringEnabled:YES]; 

[newURL release]; 
[recordSettings removeAllObjects]; 
[recordSettings release]; 
} 

以及我開始記錄方法:

- (void) startRecording { 
NSLog(@"startRecording!"); 
[micInput pause]; 
[micInput prepareToRecord]; 
micInput.meteringEnabled = YES; 
[micInput record]; 
[micInput updateMeters]; 
levelTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:0.0] interval:0.03 target:self selector:@selector(levelTimerCallback:) userInfo:nil repeats:YES]; 
[[NSRunLoop currentRunLoop] addTimer:levelTimer forMode:NSDefaultRunLoopMode]; 
} 

和位levelTimer回調:

- (void)levelTimerCallback:(NSTimer *)timer { 
[micInput updateMeters]; 
double avgPowerForChannel = pow(10, (0.05 * [micInput averagePowerForChannel:0])); 
[micSprite receiveInput:avgPowerForChannel]; 

NSLog(@"Avg. Power: %f", [micInput averagePowerForChannel:0]); 

... 

} 

凡iPhone,NSLog語句將返回明智的值,並且iPad將始終返回-120.0。

注意:我在一個cocos2d應用程序中使用了這個。出於某種原因,如果我在iPad上重新啓動當前場景,話筒電平將返回正確的值。

任何人有任何建議嗎?我在這裏非常遺憾。 謝謝!

+0

也許它會幫助你 http://stackoverflow.com/questions/24991908/why-does-averagepowerforchannel-always-return-160 – 2014-08-04 03:39:43

回答

17

我有同樣的問題。我發現將類別設置爲AVAudioSessionCategoryPlayAndRecord可以修復它:

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

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

哦,老兄,你糾正我的頭痛。 :D – 2013-09-13 08:38:03

1

是的,我也是。只有一個共享的AVAudioSession。設置其類別會影響所有錄像機和播放器,因此在應用程序的某個區域將類別設置爲AVAudioSessionCategoryPlay可禁用其他區域的錄像機。

0

另一個可能的原因很簡單,確保你打電話給[錄音機記錄];

該行被意外刪除,我們花了一段時間才意識到,作爲計數器返回-120值的原因。

HTH

0

1)請確保您有權限:

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { 
    if([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission:)]) 
    { 
     [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) { 
      NSLog(@"permission : %d", granted); 
      if(granted)[self setupMic];// record ... 
     }]; 
    } 
} 

2)確保您的路徑是合法的(的/ dev/null的OK?):

NSString* dir=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
NSArray *pathComponents = [NSArray arrayWithObjects:dir,fileName,nil]; 
recordedAudioURL = [NSURL fileURLWithPathComponents:pathComponents]; 

3)激活audioSession

[audioSession setActive:YES error:&error]; 

4)檢查所有錯誤和返回值,包括

BOOL ok=[micInput prepareToRecord]; 
ok=ok &&[micInput record]; 
0

你需要給init AVAudioSession(它應該從didMoveToview做方法,這可能是問題的原因)

let session = AVAudioSession.sharedInstance() 
    try session.setCategory(AVAudioSessionCategoryPlayAndRecord) 
    try session.overrideOutputAudioPort(AVAudioSessionPortOverride.speaker) 
    try session.setActive(true) 
try recorder = AVAudioRecorder(url: getDocumentsDirectory(), settings: settings) 

,並開始記錄

func start() { 
    recorder?.record() 
    recorder?.updateMeters() 
} 

,並通過調用

let decibels = recorder.averagePower(forChannel: 0) 
從-120(最小值)

高達一些咖啡館例如0在降噪水平得到分貝爲-20

這裏是更詳細的示例http://www.mikitamanko.com/blog/2017/04/15/swift-how-to-get-decibels/注意:也使用更新方法。