2012-08-09 110 views

回答

17

下面的代碼是從Apple's Sample AVRecorder中提取的......這段代碼從該類的movieFileOutput的連接方法獲取一組連接,獲取每個連接的AVCaptureAudioChannel,並基於此計算分貝功率。我會假設,如果你正在尋找一個輸出「噪音水平」,你將能夠捕捉到類似的信息。如果您正在尋找比此更低的級別,請嘗試HAL(硬件抽象層)框架。

- (void)updateAudioLevels:(NSTimer *)timer 
{ 
    NSInteger channelCount = 0; 
    float decibels = 0.f; 

    // Sum all of the average power levels and divide by the number of channels 
    for (AVCaptureConnection *connection in [[self movieFileOutput] connections]) { 
     for (AVCaptureAudioChannel *audioChannel in [connection audioChannels]) { 
      decibels += [audioChannel averagePowerLevel]; 
      channelCount += 1; 
     } 
    } 

    decibels /= channelCount; 

    [[self audioLevelMeter] setFloatValue:(pow(10.f, 0.05f * decibels) * 20.0f)]; 
} 
+0

該轉換不應該是pow(10.f,0.05f *分貝)嗎?在那裏你不需要額外的時間。閱讀這裏:http://stackoverflow.com/questions/2465328/iphone-sdk-avaudiorecorder-metering-how-to-change-peakpowerforchannel-from-d或更好但http://travisjeffery.com/b/2013/02 /轉換-avfoundations-power-levels-to-logarithmic-and-linear-scale/ – ucangetit 2014-05-02 10:18:40

+0

@ucangetit,我無法智慧地談論關於代碼片段的那一點細節。我只在Apple修改鏈接到示例代碼的情況下複製了代碼。 – 2014-05-02 21:30:06